New tab Sheet using fprintf function
Mostrar comentarios más antiguos
Here there,
I am trying to build code where I have a data and I want to enter the data in new/separate tab sheet for a single *.csv file where my data is filled like done in below piece of code
I would like to have new tab sheet with x data and another new tab sheet with y data.
Can some some help me
fid = fopen('data_tab_m.csv','w');
x = rand(1,3);
y = rand(3,1);
% first tab sheet
for i= 1:length(x)
fprintf('%f\n',x);
fprintf(fid,'%f\n',x);
end
% second tab sheet
for i= 1:length(y)
fprintf('%f\n',y);
fprintf(fid,'%f\n',y);
end
fclose(fid);
Respuestas (1)
Walter Roberson
el 29 de Mayo de 2023
0 votos
No, that is not possible. csv files are pure text files, and do not have tab sheets.
Putting data on other tabs is possible in .xls and .xlsx and .xlsm and .xlsb files, none of which store data in csv format.
8 comentarios
Life is Wonderful
el 29 de Mayo de 2023
Although your x and y have different orientations, the way you write them out to csv is row-wise in both cases.
filename = 'data_tab_m.xlsx';
x = rand(1,3)
y = rand(3,1)
% first tab sheet
xrep = repmat(x(:), length(x), 1).';
% second tab sheet
yrep = repmat(y(:), length(y), 1).';
%write the file
writematrix(xrep, filename, 'sheet', 1)
writematrix(yrep, filename, 'sheet', 2)
%validate
C1 = readcell(filename, 'sheet', 1)
C2 = readcell(filename, 'sheet', 2)
In short, it is easier to gather all of the data ahead of time and write it as a group.
It is possible to add data incrementally to a row in a .xls* file. You can keep track of the destination and use the 'Range' option to indicate where it should go. Or for the last few releases of MATLAB you can use 'writemode', 'append' -- but you would have to watch out for whether it appends on the same row or appends as new rows.
Life is Wonderful
el 29 de Mayo de 2023
Editada: Life is Wonderful
el 29 de Mayo de 2023
Life is Wonderful
el 30 de Mayo de 2023
Walter Roberson
el 30 de Mayo de 2023
writematrix(xrep, x1rep, filename, 'sheet', 1)
You cannot writematrix() multiple variables at the same time -- and if you could, they would certainly not go to different sheets.
There is no supported mechanism to write multiple sheets in a single call.
Even if you were using the ActiveX interface to Excel, as best I understand there would be no way to write multiple sheets in the same command.
Life is Wonderful
el 31 de Mayo de 2023
Suppose I gave you the assignment to write a Quicksort algorithm, but told you that you were not permitted to use any vowels in the code. That would be a "coding constraint".
iskeyword()
You would not be permitted (in this thought experiment) to use any of the above except spmd and try (even though arguably the y in try is acting as a vowel.)
Your position implied from the above post is that because you have this coding constraint, that MATLAB must provide alternate keywords without vowels, just because doing so would make your coding easier.
Well, NO. Just because you want to do something does not mean that MATLAB must support doing it.
The technology used for csv files does not and cannot support "tabs" or "sheets" in the sense you want. And although the technology used for .xls, .xlsx, .xlsb, .xlsm files could permit convenience functions to write out multiple variables or multiple sheets in a single call, none of the interface functions for those kinds of files do permit doing that -- not even Microsoft's own interface to Excel.
Could the writetable() interface support a struct array into which you put data and information about the location (row, column, sheet name) that the data is to be written to, and then internally handle writing all of the information into the appropriate place? Yes, it could provide an interface like that. But it doesn't .
Categorías
Más información sobre Spreadsheets en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!