How to load variables from Excel into Matlab workspace?

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 Comments

I don't see how your second method is an example of bringing Excel data into the workspace. In both methods you use writetable() which sends data to a file.
So then how would I do what I am trying to accomplish?
"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).
Yes but I might have to call out variables in the code. This is why I want to load them into the workspace.
That does not explain why you need separate variables. Just index into the table, e.g. dataTable{:,5}
Well I have a very complex Simulink model. And these variables will represent Help Text strings that I will upload to each individual block in model. So I need to be able to call them out. And I have hundreds of them so it'd be tedious to do what you're proposing.
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.
So in my Simulink, the Help Text tab can be made into a variable. But I wouldn't know how to call the specific cell to allocate the proper string (help text). This is why I'd make it a variable because everytime you compile the model, the text will update on it's own once you make the modification on the Workspace
Show us the code you are currently use to set the Help Text and perhaps we can suggest a modification.
Well there's no code involved. I simply click the 3 dots and it gives me that option to make that a variable and save it in the workspace. But the issue is, the people that will modify the actual Help Text don't have MATLAB. So I wrote a code that goes from MATLAB to Excel. Then the user will modify the text in Excel and then the idea is to load the Excel sheet and the new modifications. Thanks!
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.
The help text can be made into a variable and saved in the MATLAB workspace. You can modify the string text in the Workspace, compile the Simulink model, and the updated text string in Simulink will update. So my thinking was that if I save all the text string into an Excel sheet, I would be able to re-upload them and upon compiling the model, the Help Texts would update
But how are you compiling the model ?
Well the model was already built. So I compile it on Simulink (ctrl + B).
ctrl+B appears to invoke Simulink Coder ?
yes, but the the MATLAB workspace serves as an interface
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.
Yes, the user will not have MATLAB or access to the Simulink link. But I will. I will be in charge of updating the model. This is why I was researching Excel's compatability with MATLAB.
Well I chose Excel because it was the first thing that came to mind. A normal text editor would aslo be fine since that accesible to anyone. Only issue is that I alread wrote the code from Workspace to Excel.

Sign in to comment.

Answers (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);

Categories

Products

Asked:

on 27 Sep 2021

Edited:

on 1 Oct 2021

Community Treasure Hunt

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

Start Hunting!