How can I use Excel sheet names as variables in the area (x, y) syntax?

2 次查看(过去 30 天)
Hi there. How can I use Excel sheet names as x-variables in the area (x,y) syntax?
  1 个评论
Dyuman Joshi
Dyuman Joshi 2023-11-9
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?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-11-9
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)
ans = 3×1 string array
"s20579" "s52403" "s64206"
area(s52403, s64206)
Unrecognized function or variable 's52403'.
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 个评论
Walter Roberson
Walter Roberson 2023-11-10
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

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by