How can I use Excel sheet names as variables in the area (x, y) syntax?
1 view (last 30 days)
Show older comments
Hi there. How can I use Excel sheet names as x-variables in the area (x,y) syntax?
1 Comment
Dyuman Joshi
on 9 Nov 2023
I assume by the syntax that you want to use this particular area function which outputs a filled 2D area plot. But that function expects numeric input.
How exactly do you plan to use text data for that?
Accepted Answer
Walter Roberson
on 9 Nov 2023
You just go ahead and do it. For example,
rng(655321)
filename = 'test.xlsx';
for K = 1 : 3
sheetname = "s" + randi(65535,1,1);
writematrix(rand(5,2), filename, 'sheet', sheetname);
end
And now you can take advantage of the fact that you just happen to know the sheet names in the file to write,
sheetnames(filename)
area(s52403, s64206)
Of course this is of no benefit, since there is no variable names s52403 in the workspace.
You might know a sheet name of an excel file, but that is of little benefit to you if the data from the sheet has not been read into the workspace as some variable. And in order to use the sheet names as variable names, you have to know the sheet names ahead of time, and you have to have assigned something to those variables whos names just happen to be the same as sheet names.
I would suggest that you just Don't Do That. It is unlikely that you need to Do That.
8 Comments
Walter Roberson
on 10 Nov 2023
The part before the "now the illustration" is just preparing example data to have some file to be able to show the sequence of steps on... since you did not happen to post an example file for us to test with.
I could rewrite it to use writetable() easily enough, but this is the first time you mentioned that you are using such an old release. In that old of a release, area() did not support categorical variables.
%prepare data for illustration
rng(655321)
filename = 'test.xlsx';
for K = 1 : 3
sheetname = "data" + randi(65535,1,1);
T = array2table(rand(5,2));
writetable(T, filename, 'sheet', sheetname);
end
%now the illustration
sn = sheetnames(filename);
num_sheet = length(sn);
some_y = rand(1, num_sheet);
%do the work
x = 1:num_sheet;
area(x, some_y)
xticks(x);
xticklabels(sn); %introdued in R2016b
More Answers (0)
See Also
Categories
Find more on Spreadsheets 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!