現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Loading a .mat fil into my gui edit box
2 ビュー (過去 30 日間)
古いコメントを表示
Mikkel
2012 年 7 月 3 日
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 件のコメント
Rabia Butt
2012 年 7 月 26 日
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!
採用された回答
Walter Roberson
2012 年 7 月 3 日
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 件のコメント
Walter Roberson
2012 年 7 月 4 日
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
2012 年 7 月 4 日
編集済み: Mikkel
2012 年 7 月 4 日
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
2012 年 7 月 5 日
[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
2012 年 7 月 7 日
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
2012 年 7 月 7 日
[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
その他の回答 (1 件)
Mikkel
2012 年 7 月 7 日
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 件のコメント
Walter Roberson
2012 年 7 月 8 日
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
2012 年 7 月 8 日
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
2012 年 7 月 8 日
[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
Mikkel
2012 年 7 月 8 日
編集済み: Mikkel
2012 年 7 月 8 日
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
2012 年 7 月 8 日
for K = 1 : numel(fieldsvals2)
set( YourEditHandles2(K), 'String', num2str( fieldvals2(K) ) );
end
Mikkel
2012 年 7 月 8 日
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
2012 年 7 月 8 日
How do you initialize YourEditHandles5 ?
You only spoke before about having two sets of edit boxes, not five or more sets ?
Mikkel
2012 年 7 月 8 日
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
2012 年 7 月 8 日
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
Walter Roberson
2012 年 7 月 9 日
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
2012 年 7 月 9 日
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
2012 年 7 月 9 日
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
2012 年 7 月 9 日
編集済み: Mikkel
2012 年 7 月 9 日
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
2012 年 7 月 9 日
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
2012 年 7 月 9 日
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
2012 年 7 月 9 日
編集済み: Mikkel
2012 年 7 月 9 日
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
2012 年 7 月 9 日
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
2012 年 7 月 9 日
編集済み: Mikkel
2012 年 7 月 9 日
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
2012 年 7 月 9 日
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
参考
カテゴリ
Help Center および File Exchange で Graphics Object Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)