Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
Hi there. How can I use Excel sheet names as x-variables in the area (x,y) syntax?
  1 comentario
Dyuman Joshi
Dyuman Joshi el 9 de Nov. de 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?

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Nov. de 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)
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 comentarios
Walter Roberson
Walter Roberson el 10 de Nov. de 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
Navid
Navid el 11 de Nov. de 2023
I am grateful for your invaluable support. I could accomplish it at last.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by