How to load variables from Excel into Matlab workspace?
Mostra commenti meno recenti
clear
close
clc
aa="a1";
bb="a2";
cc="a3";
dd="a4";
ee="a5";
ff="a6";
gg="a7";
hh="a8";
ii="a9";
jj="a10";
kk="a11";
ll="a12";
mm="a13";
nn="a14";
oo="a15";
pp="a16";
qq="a17";
rr="a18";
ss="a19";
tt="a20";
uu="a21";
vv="a22";
ww="a23";
xx="a24";
yy="a25";
zz="a26";
save variables % save workspace to variables.mat
data = load('variables'); % load back in and assign to struct variable
f = fieldnames(data); % cell containing variable names
nf = numel(f); % number of variables
sz = zeros(nf,1); % array to hold dimensions of variables
% Here we get variable dimensions for each variable
for j = 1:nf
dataj = data.(f{j}); % load in variable j
% convert char arrays to string
if ischar(dataj)
dataj = convertCharsToStrings(dataj);
data.(f{j}) = dataj;
end
sz(j) = numel(dataj); % size of variable j
end
mxsz = max(sz); % max variable size
c = cell(mxsz+1,nf); % cell array to hold data
c(1,:) = f'; % column headers
for j = 1:nf
dataj = data.(f{j})(:); % variable j (turned into a column vector if necessary)
c(2:sz(j)+1,j) = num2cell(dataj); % assign to cell array
end
T = cell2table(c(2:end,:),'VariableNames',c(1,:));
writetable(T,'variables.xls')
The code above shows how to put my Workspace variables (all strings) into Excel. How can I go backwards? What if I'm given the Excel sheet with the variables, how do I load them into the Workspace. My first attempt was this
% Clean your workspace
clear
close
clc
% Create variables
aa="a1";
bb="a2";
cc="a3";
% Define to filenames
varsFile = "workspace.csv";
% Convert variables to tables
dataTable = table(aa, bb, cc);
% Write the tables to their respective files
writetable(dataTable, varsFile);
But I dont like this method because I have to call each variable and that is tedious if ihave many variables. Thanks.
18 Commenti
Missael Hernandez
il 27 Set 2021
Stephen23
il 27 Set 2021
"So then how would I do what I am trying to accomplish?"
As Matt J wrote, a better approach is to simply use keep the data in a table (and hence READTABLE and WRITETABLE).
Missael Hernandez
il 27 Set 2021
Matt J
il 27 Set 2021
That does not explain why you need separate variables. Just index into the table, e.g. dataTable{:,5}
Missael Hernandez
il 28 Set 2021
Matt J
il 28 Set 2021
I don't see how it would be more tedious. The whole reason you have tables and arrays of things in Matlab is so that many bits of data can be held together and referenced more easily than if they were held as 100s of separate variables.
Missael Hernandez
il 29 Set 2021
Matt J
il 29 Set 2021
Show us the code you are currently use to set the Help Text and perhaps we can suggest a modification.
Missael Hernandez
il 30 Set 2021
Walter Roberson
il 1 Ott 2021
the people that will modify the actual Help Text don't have MATLAB.
You are using Simulink Coder? Or are you using Simulink Compiler (an option for this purpose only since R2021b) ?
The code you posted looks like a MATLAB script, but unless things changed in R2021b, you cannot use a MATLAB script to drive a model built with Simulink Coder or Simulink Compiler.
If you have built all this into an executable, then we need to know which compile technology you used, since that will make a difference about which ways are available to create variables dynamically.
Missael Hernandez
il 1 Ott 2021
Walter Roberson
il 1 Ott 2021
But how are you compiling the model ?
Missael Hernandez
il 1 Ott 2021
Walter Roberson
il 1 Ott 2021
ctrl+B appears to invoke Simulink Coder ?
Missael Hernandez
il 1 Ott 2021
Walter Roberson
il 1 Ott 2021
You said that the users do not have MATLAB. So the users cannot go in to control+B after the new help text strings are loaded from the file. You need a MATLAB license in order to run Simulink Coder to rebuild the model.
In order to have the users be able to change the help text, you would have to have code at some Simulink level (such as the Simulink model initialization phase) that reads the Excel file and makes the appropriate change to the properties. For example you might use a Datastore and have the blocks reference something in the datastore; datastores can be updated at model initialization time.
Is there a particular reason why you want to use Excel for this purpose instead of a plain text file ? Reading an excel file inside Simulink can be a bit tricky.
Missael Hernandez
il 1 Ott 2021
Modificato: Missael Hernandez
il 1 Ott 2021
Risposte (1)
if you have many columns of data to load, you would not put them in separate workspace variables. You would just read them into a table and hold/manipulate them that way.
dataTable=readtable(varsFile);
Categorie
Scopri di più su Data Import from MATLAB in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
