You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Loading a .mat fil into my gui edit box
2 views (last 30 days)
Show older comments
Hi
Ive made a gui with alot of editboxes, and made a save button that saves the data I've put into the edixboxes, to a .mat fil.
The mat fil is a matrix due to the many edixboxes.
But now I want to make a load button.
load(uigetfile('*.mat','Select the MATLAB code file'));
This is what I have, but it doesn't load my data into the editboxes.. What more do I need to write? I have 40 editboxes.
1 Comment
Rabia Butt
on 26 Jul 2012
hey mikkel!
would you help me by telling how do you save the data you input in your editbox to a .mat file?????
I would appreciate your help! thanks!
Accepted Answer
Walter Roberson
on 3 Jul 2012
I have to make a bunch of assumptions about how you are representing your values.
[filename pathname] = uigetfile('*.mat','Select the MATLAB code file');
L = load( fullfile( pathname, filename ) );
varnames = fieldnames(L);
fieldvals = L.(varnames{1}); %GUESS: you only have 1 variable in .mat
%GUESS: the one variable is a vector of numeric values
%GUESS: you have a vector of edit box handles
%GUESS: and each box is to get the textural representation of one value
for K = 1 : length(fieldvals)
set( YourEdithandles(K), 'String', num2str( fieldvals(K) ) );
end
6 Comments
Walter Roberson
on 4 Jul 2012
One element per edit box? One row per edit box? Or is there one variable per edit box, with each variable being 7 x 10 and you want all 70 values to appear in the edit box?
In the simplest case of one element per edit box, change length(fieldvals) to numel(fieldvals)
Mikkel
on 4 Jul 2012
Edited: Mikkel
on 4 Jul 2012
If we make a simple matrix, a 3x3 :
[1 2 3
4 5 6
7 8 9]
And I have 9 edit boxes. and I want to load the numbers so editbox1 = 1, editbox2 = 2 and so like that until editbox9 = 9. How Do I do that ?
And there is one more matix into the save a 2x2
[11 22
33 44]
and I would like to save them into editbox10 = 11, editbox11 = 22, editbox12 = 33 and editbox13 = 44
What is the code?
Walter Roberson
on 5 Jul 2012
[filename pathname] = uigetfile('*.mat','Select the MATLAB code file');
L = load( fullfile( pathname, filename ) );
varnames = fieldnames(L);
fieldvals = L.(varnames{1}); %GUESS: you only have 1 variable in .mat
%GUESS: the one variable is a vector of numeric values
%GUESS: you have a vector of edit box handles
%GUESS: and each box is to get the textural representation of one value
for K = 1 : numel(fieldvals)
set( YourEdithandles(K), 'String', num2str( fieldvals(K) ) );
end
Notice that the only difference between this and what I originally posted was to change "length" to "numel", exactly as described in my comment above.
Mikkel
on 7 Jul 2012
I'm a bit lost, when you make the code general, then I don't know what to change. So the biggest help you could do for me is make it specifik for my problem. if we start from what I've made with the two matrix [3x3] and [2x2] which are called Load_soil = [3x3] and Load_dim = [2x2]
If I want to get the number 5 from the load_soil matrix into my editbox 5. What do I write?
Walter Roberson
on 7 Jul 2012
[filename pathname] = uigetfile('*.mat','Select the MATLAB code file');
L = load( fullfile( pathname, filename ) );
fieldvals = L.Load_soil;
for K = 1 : numel(fieldvals)
set( YourEdithandles(K), 'String', num2str( fieldvals(K) ) );
end
More Answers (1)
Mikkel
on 7 Jul 2012
I hope this image helps you understand my problem? Because I've done as you said (I think) or atleast as good as I can.
But it just gave me an error as you can see on the picture. Really hop you can help me. :)
22 Comments
Walter Roberson
on 8 Jul 2012
Unfortunately my system is claiming it cannot find postimage.org even though I can google it. Anyhow, could you post the image somewhere else?
Walter Roberson
on 8 Jul 2012
YourEditHandles = [handles.editbox1, handles.editbox2, handles.editbox3, handles.editbox4, handles.editbox5, handles.editbox6, handles.editbox7, handles.editbox8, handles.editbox9];
And if your actual handle names involve Editbox instead of editbox then use whatever you have, in order, so that the 5th entry in the vector corresponds to your "edit box 5"
Mikkel
on 8 Jul 2012
[filename pathname] = uigetfile('*.mat','Select the MATLAB code file');
L = load( fullfile( pathname, filename ) );
fieldvals = L.Soil_profile;
YourEditHandles = [handles.edit1, handles.edit2, handles.edit3,; handles.edit4, handles.edit5, handles.edit6,; handles.edit7, handles.edit8, handles.edit9];
for K = 1 : numel(fieldvals)
set( YourEdithandles(K), 'String', double2str( fieldvals(K) ) );
What wrong here? Thought I did what you said. It just gave me this error.
??? Undefined function or method 'YourEdithandles' for input arguments of type 'double'.
Error in ==> loadproblem>loadbutton_Callback at 402 set( YourEdithandles(K), 'String', double2str( fieldvals(K) ) );
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> loadproblem at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)loadproblem('loadbutton_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
Walter Roberson
on 8 Jul 2012
Change YourEdithandles to YourEditHandles in the set()
Mikkel
on 8 Jul 2012
Edited: Mikkel
on 8 Jul 2012
Nice it workes!!!!! but how do I get the Load_dim in also? right now its only the Soil_profile,
Do I type like this?
[filename pathname] = uigetfile('*.mat','Select the MATLAB code file');
L = load( fullfile( pathname, filename ) );
fieldvals1 = L.Soil_profile;
fieldvals2 = L.Load_Dim;
YourEditHandles1 = [handles.edit1, handles.edit2, handles.edit3,; handles.edit4, handles.edit5, handles.edit6,; handles.edit7, handles.edit8, handles.edit9];
YourEditHandles2 = [handles.edit10, handles.edit11,; handles.edit12, handles.edit13];
for K = 1 : numel(fieldvals1)
set( YourEditHandles1(K), 'String', num2str( fieldvals(K) ) );
end
for K = 2 : numel(fieldsvals2)
set( YourEditHandles2(K), 'String', num2str( fieldvals(K) ) );
end
Because it doesnt work
Walter Roberson
on 8 Jul 2012
for K = 1 : numel(fieldsvals2)
set( YourEditHandles2(K), 'String', num2str( fieldvals2(K) ) );
end
Mikkel
on 8 Jul 2012
Nice THX, now my little example works perfect! but in my original matlab gui Ive run into a little problem, the 7x10 matrix I need to put into my load, is a double.
I get this error:
??? Error using ==> set Invalid handle object.
Error in ==> boetten>load_Callback at 1483 set( YourEditHandles5(K), 'String', ( fieldvals5(K) ) );
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> boetten at 51 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)boetten('load_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
Walter Roberson
on 8 Jul 2012
How do you initialize YourEditHandles5 ?
You only spoke before about having two sets of edit boxes, not five or more sets ?
Mikkel
on 8 Jul 2012
I made a little exampel for my self so it would be easyer to explain to you and for me to understand. Everything in the exampel workes perfectly and so does my "real" matlab gui, except for the one matrix, the 7x10 matrix that is a double. That one will not load... All of the other ones workes fine, I got 7 "youredithandles" and all except that one workes perfectly
Walter Roberson
on 8 Jul 2012
Put a breakpoint at that line, and run the code. When the line is reached, at the command prompt, give the commands
size(YourEditHandles5)
ishandle(YourEditHandles5)
K
size(fieldvals5)
and let us know what it shows.
Also please show the code that constructs fieldvals5 and YourEditHandles5
Mikkel
on 8 Jul 2012
Edited: Mikkel
on 9 Jul 2012
I would like to send you my entire matlab code, think its much easyer if we both look at it, instead of me trying to explain it to you, and then trying to understand what you say :)
Ive sendt you an email
Walter Roberson
on 9 Jul 2012
I do not presently have access to MATLAB. I am trying to decide which new computer to buy, after which I will need to order a copy of MATLAB.
Mikkel
on 9 Jul 2012
Okay, Ive found the problem but dont know how to solve it, its because some of my edit boxes are "NaN" and I dont want to load them. I can't put a zero because then there will be a zero, and Ive found out I can't put NaN in. So what do I put in?
Walter Roberson
on 9 Jul 2012
What do you want to put there?
if isnan(fieldvals(K))
set( YourEditHandles(K), 'String', {''} ); %or to suit
else
set( YourEditHandles(K), 'String', num2str(fieldvals(K)) );
end
Mikkel
on 9 Jul 2012
Edited: Mikkel
on 9 Jul 2012
Hmm how can I explain it, if you look at the picture of the matrix I made of the 3x3. If I dont want anything saved into the middle, (editbox5) what do I type? because I cant just remove it, then I wouldnt be a 3x3 matrix.
If we change the matrix to:
[ 1 2 3
4 NaN 6
7 8 9]
How do I load that one?
Walter Roberson
on 9 Jul 2012
An edit box has to have something even if that something is the empty string.
The code structure I show just above would detect NaN in the input matrix and make it show as the empty string in the empty box.
Mikkel
on 9 Jul 2012
okay, let me see if I can explain this:
My original matrix looks like this.
Soil_profile = [NaN str2num(get(handles.edit1,'string')) NaN NaN NaN NaN NaN NaN NaN NaN;
NaN str2num(get(handles.edit2,'string')) NaN NaN str2num(get(handles.edit7,'string')) NaN NaN NaN NaN NaN;
NaN str2num(get(handles.edit3,'string')) NaN NaN str2num(get(handles.edit8,'string')) NaN NaN NaN NaN NaN;
NaN str2num(get(handles.edit4,'string')) NaN NaN str2num(get(handles.edit9,'string')) str2num(get(handles.edit13,'string')) str2num(get(handles.edit17,'string')) str2num(get(handles.edit21,'string')) str2num(get(handles.edit25,'string')) str2num(get(handles.edit29,'string'));
NaN str2num(get(handles.edit5,'string')) NaN NaN str2num(get(handles.edit10,'string')) str2num(get(handles.edit14,'string')) str2num(get(handles.edit18,'string')) str2num(get(handles.edit22,'string')) str2num(get(handles.edit26,'string')) str2num(get(handles.edit30,'string'));
NaN str2num(get(handles.edit6,'string')) NaN NaN str2num(get(handles.edit11,'string')) str2num(get(handles.edit15,'string')) str2num(get(handles.edit19,'string')) str2num(get(handles.edit23,'string')) str2num(get(handles.edit27,'string')) str2num(get(handles.edit31,'string'));
NaN NaN NaN NaN str2num(get(handles.edit12,'string')) str2num(get(handles.edit16,'string')) str2num(get(handles.edit20,'string')) str2num(get(handles.edit24,'string')) str2num(get(handles.edit28,'string')) str2num(get(handles.edit32,'string'))]
its easyer if you copy into a notebook and see it correctly.
THe reason why Ive put NaN into my matrix is because it use to get its data from a excel file. And now Ive made this gui with your help. The First "edixbox1" had the coordination 1,2. So I had to make a Non number for the coordinate 1,1.
But now I dont know what to do with my load. I cant change my matrix because that will take forever due to the complexity of my program. So I'm asking, what to do?
Because I really only need to load the editboxes in my matrix, but its a 7x10 matrix, and if I remove the NaN, it will not become the same matrix.
Do you understand :)? (If I where you I wouldnt ;P)
Mikkel
on 9 Jul 2012
Edited: Mikkel
on 9 Jul 2012
Remember to read the post before this one first
If i try removing the NaN in my load. Like this
YourEditHandles5 = [handles.edit1;
handles.edit2 handles.edit7;
handles.edit3 handles.edit8;
handles.edit4 handles.edit9 handles.edit13 handles.edit17 handles.edit21 handles.edit25 handles.edit29;
handles.edit5 handles.edit10 handles.edit14 handles.edit18 handles.edit22 handles.edit26 handles.edit30;
handles.edit6 handles.edit11 handles.edit15 handles.edit19 handles.edit23 handles.edit27 handles.edit31;
handles.edit12 handles.edit16 handles.edit20 handles.edit24 handles.edit28 handles.edit32]
So only the edit handles that I want to load, it gives me this error which I can understand why.
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Error in ==> boetten>load_Callback at 1485 YourEditHandles5 = [handles.edit1;
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> boetten at 51 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)boetten('load_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
But cant understand how to solve it.
Walter Roberson
on 9 Jul 2012
Use a vector, not a matrix. Especially not a matrix that cannot decide whether it is one entry per row or 7 entries per row.
Mikkel
on 9 Jul 2012
Edited: Mikkel
on 9 Jul 2012
I think I understand you, but what do I type?
NaN -34 NaN NaN NaN NaN NaN NaN NaN NaN
The first line from my matrix looks like this, so if I only take that one, its a vector.
But what do I load?
YourEditHandles5 = [handles.edit1]; <--- I tryed that, but It just gave me the 1,1 = NaN and not the -34
I need something to type infront of it.
like:
YourEditHandles5 = [xxxxxxxx.xxxxxx handles.edit1];
Help
Ive tryed NaN or 0 but nothing helps, it has to be a handle
Mikkel
on 9 Jul 2012
This is the error I got
??? Index exceeds matrix dimensions.
Error in ==> boetten>load_Callback at 1500 set( YourEditHandles5(K), 'String', num2str( fieldvals5(K) ) );
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> boetten at 51 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)boetten('load_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)