Not able to save a ".mat" file when the excel workbook is fetched within a function

2 views (last 30 days)
Hello,
I am trying to fetch an excel sheet from a function and save it as a ".mat" file but everytime I run the code, the following error comes up:
""""
Error using save
Variable 'Name' not found.
Error in test2 (line 13)
save(f,parameterList{:});
""""
Disclaimer: "Name" is the column name in the excel sheet and test2() is the function.
The following is the code:
function f = test2()
[file,path] = uigetfile(); % Fetching an excel sheet
file1 = erase(file,'.xlsx');
f = strcat(path,file1);
[data, parameterList] = test1(f);
for i = 1:length(parameterList)
assignin('base',parameterList{i},data(2:end,i));
end
save(f,parameterList{:});
end
  6 Comments
Stephen23
Stephen23 on 11 Feb 2024
Edited: Stephen23 on 11 Feb 2024
"The strange fact is that when I run the above code as a script, then there is no issue in creation and saving of the mat file. The moment I run the code within a function, the above error comes up."
It is not strange at all: presumably you run the script from the command-line in which case it uses the base workspace. You force data into the base workspace using ASSIGNIN so SAVE is therefore unable to access the data when it is called inside a function. Every function has its own workspace:
so once you force the data into some other completely unrelated workspace SAVE has nothing to work with in the function's own workspace: the data exists in the base workspace (because you forced it there) but not in the workspace where you need it to be.
That approach really should be avoided.
"How to keep all the data in one workspace ? "
Don't use ASSIGNIN to force the data into another workspace.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 11 Feb 2024
Edited: Stephen23 on 11 Feb 2024
"I am trying to fetch an excel sheet from a function and save it as a ".mat" file"
That task is much easier and much more reliable if you do not mess around with other workspaces or with magically creating variables. In short: forget about ASSIGNIN, it just makes your task harder.
P = '.'; % absolute or relative path to where the datafile is saved.
F = 'Dummy.xlsx'; % the filename
T = readtable(fullfile(P,F))
T = 3×3 table
Name Fruit Place ____ _____ _____ 1 2 3 4 5 6 7 8 9
S = table2struct(T,"ToScalar",true)
S = struct with fields:
Name: [3×1 double] Fruit: [3×1 double] Place: [3×1 double]
N = fullfile(P,replace(F,'.xlsx','.mat'));
save(N,'-struct','S')
Checking the content of the MAT file:
whos -file Dummy.mat
Name Size Bytes Class Attributes Fruit 3x1 24 double Name 3x1 24 double Place 3x1 24 double

More Answers (1)

Walter Roberson
Walter Roberson on 10 Feb 2024
Edited: Walter Roberson on 10 Feb 2024
You are saving the contents into the base workspace, but you are trying to save it in the function workspace.
cmd = "save('" + f + "', " + strjoin(parameterList, ",") + ")";
evalin('base', cmd)
  3 Comments
Stephen23
Stephen23 on 11 Feb 2024
Edited: Stephen23 on 11 Feb 2024
"What is the difference between "base" and "function" workspace ?"
"How to load contents from base to function workspace or vice-versa so that the ".mat" file is created ? "
The best way to pass data into or out of a function workspace is to pass the data as input or output arguments.
Using ASSIGNIN is not recommended, unless you want to make your code slow and complex and buggy.
But why do you need to pass that data through some other workspace? You load it into the function workspace and you want to save it from the function workspace, so why are you adding this extra complexity of passing it through some other workspace for no obvious reason?
Maya Inevitable
Maya Inevitable on 11 Feb 2024
I was not aware of this behaviour of assignin. I now get your point. Thanks a lot for the clarity!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!