r/scilab • u/mrhoa31103 • 19h ago
Another (hopefully) useful file - Findings for Reading and Writing CSV files in Scilab with mixed string and numerical data. The "uiimport" command
While working on a sample of the Data Science Course in chapter 6, using Census 1994 data, I couldn't find the .mat file on the web and only could find a .csv file. I found that Scilab really doesn't like reading .csv file with mixed data. I actually converted the .csv file to .mat file using Octave and Scilab would read the .mat file without issue.
I dug around some more to see what solution there is in Scilab and came across "uiimport(filename)' which is an interactive GUI for loading .csv files and I've found it very useful.
1) It allows you to look at the .csv in Scilab without having to fire up some other program.
2) One does not have to bring in every column from the file. Just check the ones you want.
3) .csv comes in "as a table of text" while .mat files come in "as a cell array." Conversions are required (as shown in the program).
I'm including the script for getting (massive) data in and out of Scilab via the .csv route. I'm including a short .csv data file too. The .csv read is stored in "Ans" so you need to move the data immediately or you're going to lose it when "Ans" is overwritten by the program.
Findings - Scilab .mat file writer doesn't support "varying length" string variables. It wrote without complaint with the "-v4" format but wouldn't read it back in (file is corrupt). So I'm still looking for a good way to have Scilab write .mat files that SciLab will read back in. Anyone cracked this one, send me the working file with an example.
There are two command sets for reading and writing csv files (read_csv/write_csv and csvRead/csvWrite). The first set is supposed to be the older commands however for reading strings only read_csv would do it. csvRead would produce a matrix of Nan's.
To write the data out in SciLab, one seems to be forced to write separate files for the strings and numbers as you'll see below. Make sure your header string have the correct number of columns (separated by commas) otherwise it will not read back in.
clear;
clc;
//close out all existing scf()s
close(winsid())
//Script for easily reading in multi-type csv files. Those that contain
//both string variables and numeric ones.
//
// GUI for reading CSV files - works well if you know to grab data
// from ans variable right away...
//
uiimport("C:\Users\MattheW\Documents\eBooks\ScILabNotes\DataScience\CODE\CH05\shortened_census_1994_red_col.csv")
pause
//leave the pause since you need to push the play button.
//
//type "resume after pushing PLAY on uiimport screen"
//
data = ans //grab output from uiimport in the console data is a table.
//comes is as "ans" and cannot leave it since ans is overwritten by default
//
// type "resume"
//
// Convert the table into usable column matrices
//
//
//col1 = data(:,1) //creates a column table from the main table
age = data(:,1) // separate age from the main table
age = string(age) //convert to string
age = strtod(age) // convert string to number - "this is the way."
//col2 = data(:,2)
workClass = string(data(:,2))
//col3 = data(:,3)
education_num = strtod(string(data(:,3)))
//col4 = data(:,4)
marital_status = string(data(:,4))
//col5 = data(:,5)
race = string(data(:,5))
//col6 = data(:,6)
sex = string(data(:,6))
//col7 = data(:,7)
capital_gain = strtod(string(data(:,7)))
//col8 = data(:,8)
capital_loss = strtod(string(data(:,8)))
//col9 = data(:,9)
hours_per_week = strtod(string(data(:,9)))
//col10 = data(:,10)
salary = string(data(:,10))
//Scilab cannot mix data types within a matrix so Strings and Numbers need
//seperation
//
n = size(age,1) // n is just a sample of the data to show that it works.
// Create numeric matrix
Num_output = [age(1:n), education_num(1:n), capital_gain(1:n), capital_loss(1:n), hours_per_week(1:n)]
disp('Num_output =',Num_output)
//Create a string matrix
Str_output = [workClass(1:n),marital_status(1:n),race(1:n),sex(1:n),salary(1:n)]
disp('Str_output =',Str_output)
//I couldn't write a .mat file with varying string lengths but I can write a CSV file that way
//as long as I do not intermix "string values" with numeric values
header = ["workClass, marital_status, race, sex, salary" ]; //Write the header this way and the values get
//properly distributed to the the columns
csvWrite(Str_output, "C:\Users\MattheW\Documents\eBooks\ScILabNotes\DataScience\CODE\CH05\shortened_census_1994_red_col_strout.csv", [],[],[],header)
//Note there is an older command...write_csv...
//write_csv(data, "C:\Users\MattheW\Documents\eBooks\ScILabNotes\DataScience\CODE\CH05\shortened_census_1994_red_col_strout.csv", [],[],[],header)
header = [" age, education_num, capital_gain, capital_loss, hours_per_week"]; //Write the header this way and
// the values get properly distributed to the the columns
csvWrite(Num_output, "C:\Users\MattheW\Documents\eBooks\ScILabNotes\DataScience\CODE\CH05\shortened_census_1994_red_col_numout.csv", [],[],[],header)
//NOTE: I could write a mat file using this "-v4" format but it wouldn't read back in...otherwise I got varying
// string lengths not supported. -v4 automatically pads the variables but upon reading, it says the file
// is corrupted.
//savematfile("C:\Users\MattheW\Documents\eBooks\ScILabNotes\DataScience\CODE\CH05\shortened_census_1994_red_col.m//at", 'age', 'workClass','education_num','marital_status','race','sex','capital_gain','capital_loss',...
//'hours_per_week','salary',"-v4")
//Try reading the information back in...
//
//Note: Although csvRead works for numbers, it doesn't for strings (produces Nan)
csvRead("C:\Users\MattheW\Documents\eBooks\ScILabNotes\DataScience\CODE\CH05\shortened_census_1994_red_col_numout.csv")
datanum = ans
//Need to stick with read_csv for string data...
read_csv("C:\Users\MattheW\Documents\eBooks\ScILabNotes\DataScience\CODE\CH05\shortened_census_1994_red_col_strout.csv",",","string")
datastr = ans
Short Data File:
age,workClass,education_num,marital_status,race,sex,capital_gain,capital_loss,hours_per_week,salary
39,State-gov,13,Never-married,White,Male,2174,0,40,<=50K
50,Self-emp-not-inc,13,Married-civ-spouse,White,Male,0,0,13,<=50K
38,Private,9,Divorced,White,Male,0,0,40,<=50K
53,Private,7,Married-civ-spouse,Black,Male,0,0,40,<=50K
28,Private,13,Married-civ-spouse,Black,Female,0,0,40,<=50K
37,Private,14,Married-civ-spouse,White,Female,0,0,40,<=50K
49,Private,5,Married-spouse-absent,Black,Female,0,0,16,<=50K
52,Self-emp-not-inc,9,Married-civ-spouse,White,Male,0,0,45,>50K
31,Private,14,Never-married,White,Female,14084,0,50,>50K
42,Private,13,Married-civ-spouse,White,Male,5178,0,40,>50K
37,Private,10,Married-civ-spouse,Black,Male,0,0,80,>50K
30,State-gov,13,Married-civ-spouse,Asian-Pac-Islander,Male,0,0,40,>50K
23,Private,13,Never-married,White,Female,0,0,30,<=50K
32,Private,12,Never-married,Black,Male,0,0,50,<=50K
40,Private,11,Married-civ-spouse,Asian-Pac-Islander,Male,0,0,40,>50K
34,Private,4,Married-civ-spouse,Amer-Indian-Eskimo,Male,0,0,45,<=50K
25,Self-emp-not-inc,9,Never-married,White,Male,0,0,35,<=50K
32,Private,9,Never-married,White,Male,0,0,40,<=50K
38,Private,7,Married-civ-spouse,White,Male,0,0,50,<=50K
43,Self-emp-not-inc,14,Divorced,White,Female,0,0,45,>50K
Try it out.






















































