error in calling a variable from another mat file

조회 수: 1 (최근 30일)
summyia qamar
summyia qamar 2017년 1월 28일
답변: Jan 2017년 1월 29일
after doing some coding I have run a loop and saved values in cell R
for r=1:En
R{r} =E(Ec+Ea(r,:),:);
end
I have saved a variable of cell in a matfile named
save('data_12x6','R')
where 'data_12x6' is fle name and 'R'is variable . now I want to call this variable in the below code
for r=1:numel(R)
X=(R{r}*x).*y';
XX(r)=(sum(X))*0.01;
end
I an trying this
R=load('data_12x6','R')
for r=1:numel(R)
X=(R{r}*x).*y';
XX(r)=(sum(X))*0.01;
end
but it is giving an error that
Warning: Variable 'R' not found.
R =
struct with no fields.
Cell contents reference from a
non-cell array object.
how can I do this?
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 1월 28일
R=load('data_12x6','R')
would always give back a struct in that form of load(). There are forms of load() that return back values instead of a struct, but you cannot request particular variables when you use them.
The warning is telling you that it did not find a variable R inside data_12x6.mat
Walter Roberson
Walter Roberson 2017년 1월 29일
Please show the output of
whos -file data_12x6

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

채택된 답변

Jan
Jan 2017년 1월 29일
R = rand;
save('data_12x6', 'R');
matfile('data_12x6')
>> Properties:
>> Properties.Source: 'D:\MFiles\data_12x6.mat'
>> Properties.Writable: false
>> R: [1x1 double]
Q = load('data_12x6')
>> Q.R = 0.8651243
Q = load('data_12x6', 'R')
>> Q.R = 0.8651243
Q = load('data_12x6', 'Miss')
>> Warning: Variable 'Miss' not found.
>> Q =
>> struct with no fields.
This means, that your data_12x6 file does not contain the variable R. This can happen only, if it was not created by |save('data_12x6', 'R'). I guess, that the current folder has changed. Prefer to use absolute path names:
folder = tempdir; % Or where you want to store the files
save(fullfile(folder, 'data_12x6.mat'), 'R');
...
FileData = load(fullfile(folder, 'data_12x6.mat'));
R = FileData.R;

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by