Array within a structure array doesn't hold previous values

Hi Again, Initially a structure was created with following way
Student=struct('Class','Junior','ID',1631,'Name','XYZ','Fees',0);
save('file.mat','Student','-v7.3');
It was saved in D:\, then In my GUI I am accessing and updating this structure as
% Respective edit boxex are read as
id = str2double(get(handles.IDBox,'String'));
name = get(handles.NameBox,'String');
.
.
.
Month = str2double(DoA(4:5));%DoA is date of admission
fees = str2double(get(handles.FeesBox,'String'));% Fee edit box read
FeesArray(Month) = fees;% Current month fee is added in FeeArray
%above values are saved in structure as
filename=fullfile('D:\file.mat');
h=matfile(filename,'Writable',true);
h.Student(id,:)=Student{id};
All fields are updated correctly but Fees array retains only last updated value and previous values are replaced with 0.
Please help how can I program to retain all values in Fee Array ?

2 件のコメント

Jan
Jan 2017 年 3 月 12 日
編集済み: Jan 2017 年 3 月 12 日
If "Student" is a "struct" (not "Structure"), accessing it by curly braces would cause an error. Therefore I assume, that "Student" is a cell. What is "id"? Please edit the original question and add the additional details. Thanks.
Muhammad Abdurrehman
Muhammad Abdurrehman 2017 年 3 月 13 日
編集済み: Muhammad Abdurrehman 2017 年 3 月 13 日
Hi Jan,
Thanks a lot for answering, Same effect if we use parenthesis instead of curly brackets. I have updated the question please have a review. Thanks

サインインしてコメントする。

回答 (2 件)

Guillaume
Guillaume 2017 年 3 月 12 日

0 投票

At the bottom of the documentation for matfile, it says:
matfile does not support indexing into:
[...]
Fields of structure arrays
[...]
You have to replace the whole field.

4 件のコメント

Muhammad Abdurrehman
Muhammad Abdurrehman 2017 年 3 月 13 日
Hi,
Hmmmm. Lets wait for Jan Simon if he finds some alternate
Guillaume
Guillaume 2017 年 3 月 13 日
Having looked at your new code, it doesn't look like you're indexing into the fields of the structure, but are actually replacing a whole row of a structure array, which should be fine.
Jan
Jan 2017 年 3 月 13 日
Sorry, I don't get the problem. What is "Student{id}" in this line:
h.Student(id,:)=Student{id};
? In the test you speak about the "Fee array". Is this "fees" or "FeesArray(Month)"? Is "FeesArray(Month)" a vector of zeros except for the last index at position Month?
Muhammad Abdurrehman
Muhammad Abdurrehman 2017 年 3 月 14 日
Hi, thanks for the reply. Yes this is the issue
FeesArray(Month)" a vector of zeros except for the last index at position Month. That's why it saves only last value in Structure Field i.e. Fees. Can you please suggest how to save only current value while unchanging previously entered ?

サインインしてコメントする。

Guillaume
Guillaume 2017 年 3 月 13 日
編集済み: Guillaume 2017 年 3 月 13 日

0 投票

Starting a new answer, since it looks like my initial answer was a misunderstanding...
h.Student(id, :) = ...
would indicate that the Student variable in the mat file is a 2D array. I would assume it is a 2D structure array although your code shows it being saved as a scalar structure.
= ... Student{id}
would indicate that the Student variable in the current workspace is a cell array. Presumably, the cell array contains structures but why isn't it structure array like in the mat file?
So, it looks like you're replacing a whole row of a structure array. I'm unclear what problem you're having. Aren't the values in the whole row identical after your update?

4 件のコメント

Muhammad Abdurrehman
Muhammad Abdurrehman 2017 年 3 月 14 日
Hi, Thanks for the answer curly braces are replaced with parenthesis. Let me explain my question in few lines....
I want to make a structure that should have an array field with named as Fee. So that this field could be accessed as
CurrentMonthFee=Student(id,:).Fee(Month);
the above line works good. But the issue is with saving the values in this array (i.e. Fee). So my question is how can I save values in Array field of Structure. (The maximum values are 12 and a new value should be added when I press Update button in my GUI.) Currently when I press Update Button it only holds the last entered value are previously entered values are replaced with 0.
Guillaume
Guillaume 2017 年 3 月 14 日
"the above line works good". Hum, no! At best it is confusing, at worst it will result in an error. If size(Student, 2) is 1 then the ,: is confusing as it implies that Student has more than one column when it hasn't, so:
CurrentMonthFee = Student(id).Fee(Month); %Don't use (id, :)
If size(Student, 2) is greater than 1, then Student(id, :).Fee will return a comma separated list of the Fee field of the whole row. You can't index comma separated lists, so the line will result in an error: Expected one output from a curly brace or dot expression ....
As for your question, without seeing the code you're using, it's difficult to tell what you're doing wrong.
Muhammad Abdurrehman
Muhammad Abdurrehman 2017 年 3 月 14 日
actually when you are accessing your structure via a handler (created using matfile) then you have to use (id,:) because it doesn't support linear indexing.
Guillaume
Guillaume 2017 年 3 月 14 日
編集済み: Guillaume 2017 年 3 月 14 日
I'm utterly confused. With this line:
CurrentMonthFee=Student(id,:).Fee(Month);
you are not accessing anything using a matfile. And if you were with e.g.:
CurrentMonthFee = h.Student(id,:).Fee(Month); %I would use 1 instead of :
the .Fee part would throw an error.
With no valid code to go with, I still have no idea what you're trying to do. If you are trying to modify the monthly fee for a particular student:
h = matfile(filename, 'Writable', true);
%id: student id
%Month: Month to replace
%fee: value to assign
student = h.Student(id, 1);
student.Fee(Month) = fee;
h.Student(id, 1) = student;
You have to use a temporary variable to modify the fields of a structure through matfile. As per my 1st answer, it is explictly stated in the doc that you cannot index into the fields of a structure.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeHistorical Contests についてさらに検索

質問済み:

2017 年 3 月 12 日

編集済み:

2017 年 3 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by