You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
New tab Sheet using fprintf function
9 views (last 30 days)
Show older comments
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
0.119879
0.141342
0.281086
0.119879
0.141342
0.281086
0.119879
0.141342
0.281086
% second tab sheet
for i= 1:length(y)
fprintf('%f\n',y);
fprintf(fid,'%f\n',y);
end
0.523532
0.924488
0.376526
0.523532
0.924488
0.376526
0.523532
0.924488
0.376526
fclose(fid);
Answers (1)
Walter Roberson
on 29 May 2023
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 Comments
Life is Wonderful
on 29 May 2023
allright, *.csv is clean when compared to other file format. Then I am Okay to have in .xlsx , can you please help me with a logic !!
Thank you
Walter Roberson
on 29 May 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)
x = 1×3
0.1153 0.1630 0.5667
y = rand(3,1)
y = 3×1
0.8233
0.6259
0.9962
% 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)
Warning: Added specified worksheet.
%validate
C1 = readcell(filename, 'sheet', 1)
C1 = 1×9 cell array
{[0.1153]} {[0.1630]} {[0.5667]} {[0.1153]} {[0.1630]} {[0.5667]} {[0.1153]} {[0.1630]} {[0.5667]}
C2 = readcell(filename, 'sheet', 2)
C2 = 1×9 cell array
{[0.8233]} {[0.6259]} {[0.9962]} {[0.8233]} {[0.6259]} {[0.9962]} {[0.8233]} {[0.6259]} {[0.9962]}
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
on 29 May 2023
Edited: Life is Wonderful
on 29 May 2023
Thank you very much!! Looks excellent and is simple to adapt, but your suggestion does not assist because I am unable to fit more items because I am adapting to fprintf style.
Is there a way to include data header label saying x , x1 x2 ... and x so on for sheet tab 1 and in similarly tab2 having hearder y,y1,y2, ... and so on for each data.
Because I am unable to add many variables in the same way that I can print more outdata in the files, the proposal has a defect or I did not caputre in my requirement correctly. I require your assistance here to allow extra variables for sheet 1 as I anticipate; writematrix works for me similarly to fprintf.
filename = 'data_tab_m.xlsx';
x = rand(1,3)
x = 1×3
0.4771 0.2721 0.6802
x1 = rand(1,3)
x1 = 1×3
0.6408 0.9940 0.8319
% first tab sheet, first variable
xrep = repmat(x(:), length(x), 1).';
% first tab sheet, second variable
x1rep = repmat(x1(:), length(x1), 1).';
%write the file having two varaibles
writematrix(xrep, x1rep, filename, 'sheet', 1)
Error using strlength
First argument must be text.
First argument must be text.
Error in writematrix (line 177)
n = strlength(varargin{i});
Life is Wonderful
on 30 May 2023
@Walter Roberson, Do you have any suggestions for me?
Thank you!!
Walter Roberson
on 30 May 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
on 31 May 2023
However, you have a coding constraint.
By constructing a tab with numerous data as there is a need to analyse groups of particular information under a group such like makes math easy, and if the plan is to make individual variables under a single tab sheet, that is not useful.
Regards for your help
Walter Roberson
on 31 May 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()
ans = 20×1 cell array
{'break' }
{'case' }
{'catch' }
{'classdef' }
{'continue' }
{'else' }
{'elseif' }
{'end' }
{'for' }
{'function' }
{'global' }
{'if' }
{'otherwise' }
{'parfor' }
{'persistent'}
{'return' }
{'spmd' }
{'switch' }
{'try' }
{'while' }
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 .
Life is Wonderful
on 1 Jun 2023
So, we both have great things to say, and the true use case is to provide a solution, and we both agree that one spreadsheet cannot support numerous tabs with multiple variables to write and read.
I will go another route and give data analysis where print includes multi-location variable print that collaborates data in the same area.
In the example code below, I organised all variables into a structure and can print all essential data in text, *.xls, *.xlsx, *.csv and so on.
In below example, say multi_data_analysis() does addition, multiplication, and data collection on a single sheet.
clc;clearvars;
fid = fopen('collect_data.csv','w');
T.a = 1:10;
T.b = 1:2:20;
T.c = 1:3:30;
T.d = 1:4:40;
clear T.x T.y;
fprintf('%10s|%10s|%10s|%10s|%10sd|\n----------+----------+----------+----------+----------+\n','i','j','a(i)','b(j)','x(i,J)');
i| j| a(i)| b(j)| x(i,J)d|
----------+----------+----------+----------+----------+
[T] = math_add(T);
1| 1| 1| 1| 2|
1| 2| 1| 3| 4|
1| 3| 1| 5| 6|
1| 4| 1| 7| 8|
1| 5| 1| 9| 10|
1| 6| 1| 11| 12|
1| 7| 1| 13| 14|
1| 8| 1| 15| 16|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7| 8| 13| 21|
8| 8| 8| 15| 23|
8| 9| 8| 17| 25|
8| 10| 8| 19| 27|
9| 1| 9| 1| 10|
9| 2| 9| 3| 12|
9| 3| 9| 5| 14|
9| 4| 9| 7| 16|
9| 5| 9| 9| 18|
9| 6| 9| 11| 20|
9| 7| 9| 13| 22|
9| 8| 9| 15| 24|
9| 9| 9| 17| 26|
9| 10| 9| 19| 28|
10| 1| 10| 1| 11|
10| 2| 10| 3| 13|
10| 3| 10| 5| 15|
10| 4| 10| 7| 17|
10| 5| 10| 9| 19|
10| 6| 10| 11| 21|
10| 7| 10| 13| 23|
10| 8| 10| 15| 25|
10| 9| 10| 17| 27|
10| 10| 10| 19| 29|
fprintf('%10s|%10s|%10s|%10s|%10sd|\n----------+----------+----------+----------+----------+\n','i','j','c(i)','c(j)','y(i,J)');
i| j| c(i)| c(j)| y(i,J)d|
----------+----------+----------+----------+----------+
[T] = math_mul(T);
1| 1| 1| 1| 1|
1| 2| 1| 5| 5|
1| 3| 1| 9| 9|
1| 4| 1| 13| 13|
1| 5| 1| 17| 17|
1| 6| 1| 21| 21|
1| 7| 1| 25| 25|
1| 8| 1| 29| 29|
1| 9| 1| 33| 33|
1| 10| 1| 37| 37|
2| 1| 4| 1| 4|
2| 2| 4| 5| 20|
2| 3| 4| 9| 36|
2| 4| 4| 13| 52|
2| 5| 4| 17| 68|
2| 6| 4| 21| 84|
2| 7| 4| 25| 100|
2| 8| 4| 29| 116|
2| 9| 4| 33| 132|
2| 10| 4| 37| 148|
3| 1| 7| 1| 7|
3| 2| 7| 5| 35|
3| 3| 7| 9| 63|
3| 4| 7| 13| 91|
3| 5| 7| 17| 119|
3| 6| 7| 21| 147|
3| 7| 7| 25| 175|
3| 8| 7| 29| 203|
3| 9| 7| 33| 231|
3| 10| 7| 37| 259|
4| 1| 10| 1| 10|
4| 2| 10| 5| 50|
4| 3| 10| 9| 90|
4| 4| 10| 13| 130|
4| 5| 10| 17| 170|
4| 6| 10| 21| 210|
4| 7| 10| 25| 250|
4| 8| 10| 29| 290|
4| 9| 10| 33| 330|
4| 10| 10| 37| 370|
5| 1| 13| 1| 13|
5| 2| 13| 5| 65|
5| 3| 13| 9| 117|
5| 4| 13| 13| 169|
5| 5| 13| 17| 221|
5| 6| 13| 21| 273|
5| 7| 13| 25| 325|
5| 8| 13| 29| 377|
5| 9| 13| 33| 429|
5| 10| 13| 37| 481|
6| 1| 16| 1| 16|
6| 2| 16| 5| 80|
6| 3| 16| 9| 144|
6| 4| 16| 13| 208|
6| 5| 16| 17| 272|
6| 6| 16| 21| 336|
6| 7| 16| 25| 400|
6| 8| 16| 29| 464|
6| 9| 16| 33| 528|
6| 10| 16| 37| 592|
7| 1| 19| 1| 19|
7| 2| 19| 5| 95|
7| 3| 19| 9| 171|
7| 4| 19| 13| 247|
7| 5| 19| 17| 323|
7| 6| 19| 21| 399|
7| 7| 19| 25| 475|
7| 8| 19| 29| 551|
7| 9| 19| 33| 627|
7| 10| 19| 37| 703|
8| 1| 22| 1| 22|
8| 2| 22| 5| 110|
8| 3| 22| 9| 198|
8| 4| 22| 13| 286|
8| 5| 22| 17| 374|
8| 6| 22| 21| 462|
8| 7| 22| 25| 550|
8| 8| 22| 29| 638|
8| 9| 22| 33| 726|
8| 10| 22| 37| 814|
9| 1| 25| 1| 25|
9| 2| 25| 5| 125|
9| 3| 25| 9| 225|
9| 4| 25| 13| 325|
9| 5| 25| 17| 425|
9| 6| 25| 21| 525|
9| 7| 25| 25| 625|
9| 8| 25| 29| 725|
9| 9| 25| 33| 825|
9| 10| 25| 37| 925|
10| 1| 28| 1| 28|
10| 2| 28| 5| 140|
10| 3| 28| 9| 252|
10| 4| 28| 13| 364|
10| 5| 28| 17| 476|
10| 6| 28| 21| 588|
10| 7| 28| 25| 700|
10| 8| 28| 29| 812|
10| 9| 28| 33| 924|
10| 10| 28| 37| 1036|
fprintf(['%10s|%10s|%10s|%10s|%10s|%10s|%10s|%10sd|\n' ...
'----------+----------+----------+----------+----------+----------+----------+-----------+\n'], ...
'u','v','T.a(u)','T.b(u)','T.c(u)','T.d(u)','T.x(u,v)','T.y(u,v)');
u| v| T.a(u)| T.b(u)| T.c(u)| T.d(u)| T.x(u,v)| T.y(u,v)d|
----------+----------+----------+----------+----------+----------+----------+-----------+
fprintf(fid,['%10s, %10s, %10s, %10s, %10s, %10s, %10s, %10s\n'], ...
'u','v','T.a(u)','T.b(u)','T.c(u)','T.d(u)','T.x(u v)','T.y(u v)');
for u = 1:size(T.x,1)
for v = 1:size(T.x,1)
fprintf('%10d|%10d|%10d|%10d|%10d|%10d|%10d|%10d|\n',u,v,T.a(u),T.b(u),T.c(u),T.d(u),T.x(u,v),T.y(u,v));
fprintf(fid,'%10d, %10d, %10d, %10d, %10d, %10d, %10d, %10d, \n',u,v,T.a(u),T.b(u),T.c(u),T.d(u),T.x(u,v),T.y(u,v));
end
end
1| 1| 1| 1| 1| 1| 2| 1|
1| 2| 1| 1| 1| 1| 4| 5|
1| 3| 1| 1| 1| 1| 6| 9|
1| 4| 1| 1| 1| 1| 8| 13|
1| 5| 1| 1| 1| 1| 10| 17|
1| 6| 1| 1| 1| 1| 12| 21|
1| 7| 1| 1| 1| 1| 14| 25|
1| 8| 1| 1| 1| 1| 16| 29|
1| 9| 1| 1| 1| 1| 18| 33|
1| 10| 1| 1| 1| 1| 20| 37|
2| 1| 2| 3| 4| 5| 3| 4|
2| 2| 2| 3| 4| 5| 5| 20|
2| 3| 2| 3| 4| 5| 7| 36|
2| 4| 2| 3| 4| 5| 9| 52|
2| 5| 2| 3| 4| 5| 11| 68|
2| 6| 2| 3| 4| 5| 13| 84|
2| 7| 2| 3| 4| 5| 15| 100|
2| 8| 2| 3| 4| 5| 17| 116|
2| 9| 2| 3| 4| 5| 19| 132|
2| 10| 2| 3| 4| 5| 21| 148|
3| 1| 3| 5| 7| 9| 4| 7|
3| 2| 3| 5| 7| 9| 6| 35|
3| 3| 3| 5| 7| 9| 8| 63|
3| 4| 3| 5| 7| 9| 10| 91|
3| 5| 3| 5| 7| 9| 12| 119|
3| 6| 3| 5| 7| 9| 14| 147|
3| 7| 3| 5| 7| 9| 16| 175|
3| 8| 3| 5| 7| 9| 18| 203|
3| 9| 3| 5| 7| 9| 20| 231|
3| 10| 3| 5| 7| 9| 22| 259|
4| 1| 4| 7| 10| 13| 5| 10|
4| 2| 4| 7| 10| 13| 7| 50|
4| 3| 4| 7| 10| 13| 9| 90|
4| 4| 4| 7| 10| 13| 11| 130|
4| 5| 4| 7| 10| 13| 13| 170|
4| 6| 4| 7| 10| 13| 15| 210|
4| 7| 4| 7| 10| 13| 17| 250|
4| 8| 4| 7| 10| 13| 19| 290|
4| 9| 4| 7| 10| 13| 21| 330|
4| 10| 4| 7| 10| 13| 23| 370|
5| 1| 5| 9| 13| 17| 6| 13|
5| 2| 5| 9| 13| 17| 8| 65|
5| 3| 5| 9| 13| 17| 10| 117|
5| 4| 5| 9| 13| 17| 12| 169|
5| 5| 5| 9| 13| 17| 14| 221|
5| 6| 5| 9| 13| 17| 16| 273|
5| 7| 5| 9| 13| 17| 18| 325|
5| 8| 5| 9| 13| 17| 20| 377|
5| 9| 5| 9| 13| 17| 22| 429|
5| 10| 5| 9| 13| 17| 24| 481|
6| 1| 6| 11| 16| 21| 7| 16|
6| 2| 6| 11| 16| 21| 9| 80|
6| 3| 6| 11| 16| 21| 11| 144|
6| 4| 6| 11| 16| 21| 13| 208|
6| 5| 6| 11| 16| 21| 15| 272|
6| 6| 6| 11| 16| 21| 17| 336|
6| 7| 6| 11| 16| 21| 19| 400|
6| 8| 6| 11| 16| 21| 21| 464|
6| 9| 6| 11| 16| 21| 23| 528|
6| 10| 6| 11| 16| 21| 25| 592|
7| 1| 7| 13| 19| 25| 8| 19|
7| 2| 7| 13| 19| 25| 10| 95|
7| 3| 7| 13| 19| 25| 12| 171|
7| 4| 7| 13| 19| 25| 14| 247|
7| 5| 7| 13| 19| 25| 16| 323|
7| 6| 7| 13| 19| 25| 18| 399|
7| 7| 7| 13| 19| 25| 20| 475|
7| 8| 7| 13| 19| 25| 22| 551|
7| 9| 7| 13| 19| 25| 24| 627|
7| 10| 7| 13| 19| 25| 26| 703|
8| 1| 8| 15| 22| 29| 9| 22|
8| 2| 8| 15| 22| 29| 11| 110|
8| 3| 8| 15| 22| 29| 13| 198|
8| 4| 8| 15| 22| 29| 15| 286|
8| 5| 8| 15| 22| 29| 17| 374|
8| 6| 8| 15| 22| 29| 19| 462|
8| 7| 8| 15| 22| 29| 21| 550|
8| 8| 8| 15| 22| 29| 23| 638|
8| 9| 8| 15| 22| 29| 25| 726|
8| 10| 8| 15| 22| 29| 27| 814|
9| 1| 9| 17| 25| 33| 10| 25|
9| 2| 9| 17| 25| 33| 12| 125|
9| 3| 9| 17| 25| 33| 14| 225|
9| 4| 9| 17| 25| 33| 16| 325|
9| 5| 9| 17| 25| 33| 18| 425|
9| 6| 9| 17| 25| 33| 20| 525|
9| 7| 9| 17| 25| 33| 22| 625|
9| 8| 9| 17| 25| 33| 24| 725|
9| 9| 9| 17| 25| 33| 26| 825|
9| 10| 9| 17| 25| 33| 28| 925|
10| 1| 10| 19| 28| 37| 11| 28|
10| 2| 10| 19| 28| 37| 13| 140|
10| 3| 10| 19| 28| 37| 15| 252|
10| 4| 10| 19| 28| 37| 17| 364|
10| 5| 10| 19| 28| 37| 19| 476|
10| 6| 10| 19| 28| 37| 21| 588|
10| 7| 10| 19| 28| 37| 23| 700|
10| 8| 10| 19| 28| 37| 25| 812|
10| 9| 10| 19| 28| 37| 27| 924|
10| 10| 10| 19| 28| 37| 29| 1036|
function [T] = math_add(T)
fid = fopen('collect_add.csv','w');
fprintf(fid,'%10s, %10s, %10s, %10s, %10sd, \n','i','j','a(i)','b(j)','x(i J)');
for i = 1:size(T.a,2)
for j = 1:size(T.b,2)
T.x(i,j) = T.a(i) + T.b(j);
fprintf('%10d|%10d|%10d|%10d|%10d|\n',i,j,T.a(i),T.b(j),T.x(i,j));
fprintf(fid,'%10d, %10d, %10d, %10d, %10d, \n',i,j,T.a(i),T.b(j),T.x(i,j));
end
end
fclose(fid);
end
function [T] = math_mul(T)
fid = fopen('collect_mul.csv','w');
fprintf(fid,'%10s, %10s, %10s, %10s, %10sd \n','i','j','c(i)','d(j)','y(i J)');
for i = 1:size(T.c,2)
for j = 1:size(T.d,2)
T.y(i,j) = T.c(i) .* T.d(j);
fprintf('%10d|%10d|%10d|%10d|%10d|\n',i,j,T.c(i), T.d(j),T.y(i,j));
fprintf(fid,'%10d, %10d, %10d, %10d, %10d, \n',i,j,T.c(i), T.d(j),T.y(i,j));
end
end
fclose(fid);
end
Thank you
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)