MATLAB Answers

Array within a structure array doesn't hold previous values

1 view (last 30 days)
Abdurrehman on 12 Mar 2017
Edited: Guillaume on 14 Mar 2017
Hi Again, Initially a structure was created with following way
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
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 ?
Muhammad Abdurrehman
Muhammad Abdurrehman on 13 Mar 2017
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

Sign in to comment.

Answers (2)

Guillaume on 12 Mar 2017
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.
Muhammad Abdurrehman
Muhammad Abdurrehman on 14 Mar 2017
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 ?

Sign in to comment.

Guillaume on 13 Mar 2017
Edited: Guillaume on 13 Mar 2017
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?
Guillaume on 14 Mar 2017
I'm utterly confused. With this line:
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!