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

조회 수: 1 (최근 30일)
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
Stephen23
Stephen23 2024년 2월 11일
편집: Stephen23 2024년 2월 11일
"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.
Maya Inevitable
Maya Inevitable 2024년 2월 11일
Thank you for your response. It was really helpful.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2024년 2월 11일
편집: Stephen23 2024년 2월 11일
"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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2024년 2월 10일
편집: Walter Roberson 2024년 2월 10일
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
Stephen23
Stephen23 2024년 2월 11일
편집: Stephen23 2024년 2월 11일
"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 2024년 2월 11일
I was not aware of this behaviour of assignin. I now get your point. Thanks a lot for the clarity!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by