trying to pad smaller table to larger table
Show older comments
i have a csv file that table thats a (20 x 30,0000) and i calculated data thats (1 x 27) and need to add that to that csv file. im stuck on what to do and cant seem to figure this out.
code is:
im sure im doing something wrong but have no clue what it could be.
Table1 = [Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70];
PD = padarray(Table1,[1,0],0)
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_new = [T_preserve, PD];
writetable(T_new, 'Tesla_Steady_State_027.csv');
1 Comment
Walter Roberson
on 2 Nov 2020
i have a csv file that table thats a (20 x 30,0000)
Could you confirm that you have a table that has 20 rows with 30000 variables? And that you want to add one row with 27 variables?
Accepted Answer
More Answers (1)
Walter Roberson
on 2 Nov 2020
The below code accounts for the possibility of either table having fewer rows than the other one.
Table1 = [Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70];
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_preserve{end+1:height(Table1),:} = missing;
Table1{end+1:height(T_preserve), :} = missing;
T_new = [T_preserve, PD];
writetable(T_new, 'Tesla_Steady_State_027.csv');
16 Comments
Unfortunately missing only fills missing values of some data types. For example, if the table contains cells or graphics handles, the missing value is not defined and will throw an error.
Example:
T = table(num2cell((1:3)'), gobjects(3,1), (1:3)')
try
T{6:7,:} = missing;
catch ME
fprintf(2, [ME.message,newline])
end
T(:,1) = []; % Remove col 1
try
T{6:7,:} = missing;
catch ME
fprintf(2, [ME.message,newline])
end
The outerjoin method gets around the problem but requires that key columns are added or identified.
T1 = table(num2cell((1:3)'), gobjects(3,1), (1:3)')
T2 = table(num2cell((1:5)'), gobjects(5,1), (1:5)')
% Add keys
T1.KEY = (1:height(T1))';
T2.KEY = (1:height(T2))';
% Horizontally concatenate
TCAT = outerjoin(T1,T2, 'Keys', 'KEY');
% Remove keys
TCAT(:, cumsum([width(T2), width(T2)])) = []
isamh
on 5 Nov 2020
writetable with sheet name-value pair.
Walter Roberson
on 5 Nov 2020
you cannot provide sheet range for text output, only for spreadsheet output.
isamh
on 5 Nov 2020
Walter Roberson
on 5 Nov 2020
csv does not have sheets.
And earlier I discussed why it is literally not possible to add additional columns to a text file without rewriting the entire file.
You cannot use xlsx as a "master" file to make manipulation easier and then export to csv later, because you said that you have 20 rows and 300000 columns, but the maximum supported by xlsx format is 16384 columns.
isamh
on 5 Nov 2020
Walter Roberson
on 5 Nov 2020
No, I do not have any ideas that will help. My ideas are along the lines that you should not be trying to store different dimensions of data in a single csv, that csv were never designed for that. Your data organization appears to be confused, and that looks to reflect confusion about what data is to be stored at all.
Why are you using csv?
isamh
on 5 Nov 2020
Adam Danz
on 5 Nov 2020
You can save them to different pages of an excel spreadsheet by using writetable along with the extension .xls, .xlsm, or .xlsx etc.
Walter Roberson
on 5 Nov 2020
With regards to speeding things up:
.xlsx files are internally a zip'd directory of XML text files. There are some file that give information about what is being stored, and there is one file that is sometimes used to store strings that occur a lot in the data, and there is one file per sheet. The processing of these files involves a lot of text processing to turn the data into numeric form.
Therefore if "speeding things up" is in the sense of making it faster to read data in, then .xlsx files are not the way to go. .xlsx files are convenient and more-or-less portable, but they are not high performance. For pure numeric data, reading csv files using textscan or readmatrix can be faster.
If you have multiple input files, then merging the files together can sometimes be easier for the programmer to process, and faster coding with easier debugging is something real to talk about "help or speed things up". However, if you merge too much information into the same file, then extracting the part you want from the file can take a lot of complicated coding, which slows you right down again.
As a general design principle: data that represents different kinds of things should not go into the same variable -- not unless you are using some kind of structuring that gives information about what the data represents.
Taking the time to read data from csv and store it into a .mat file can be worthwhile if the data is going to be processed multiple times.
Before you decide how to store the variables, you should probably think more about how the data is going to be used, taking into account that it obviously is not all comparable -- not all measurements at a list of pre-determined times for example (otherwise the data you were trying to add on would have the same number of rows or the same number of columns as the data you already had.)
isamh
on 5 Nov 2020
Adam Danz
on 5 Nov 2020
100% agree with WR.
isamh
on 6 Nov 2020
Adam Danz
on 8 Nov 2020
Check out the 'range' parameter for writetable().
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!