uigetfile, load m-file variables into workspace

17 ビュー (過去 30 日間)
Jan w
Jan w 2016 年 12 月 17 日
編集済み: Image Analyst 2017 年 1 月 2 日
Hello,
I've been trying to load variables from a *.m file into my workspace with the following code provided by Support Team by clicking the pushbutton on my gui.
function profil_laden_Callback(hObject, eventdata, handles)
filename = uigetfile('*.mat');
command = sprintf('load(''%s'')', filename);
evalin('base', command);
But i get the error:
Error using load
Number of columns on line 3 of ASCII file Eingabe.m must be the same as previous lines.
My m file is a editable file for users, so they can put their values in. It is full of comments.
Thanks
  1 件のコメント
Stephen23
Stephen23 2016 年 12 月 17 日
編集済み: Stephen23 2016 年 12 月 17 日
evalin is the worst way of passing data between workspaces:
and evalin, like eval, is slow and buggy:
Much faster and more robust would be to load the data into a structure, and and use nested functions to pass the data between the workspaces.

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

採用された回答

Image Analyst
Image Analyst 2016 年 12 月 17 日
編集済み: Image Analyst 2017 年 1 月 2 日
Do it like this instead
[baseFileName, folder] = uigetfile('*.mat');
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
% Normal situation - they picked an existing file.
storedStructure = load(fullFileName);
% Now do something with storedStructure, like extract fields into new variables or whatever you want.
else
% Error: Would only get here if they typed in a name of a non-existant file
% instead of picking one from the folder.
warningMessage = sprintf('Warning: mat file does not exist:\n%s', fullFileName);
uiwait(errordlg(warningMessage));
return;
end
Note that the variables in a callback function are local. If you want them to be visible (in scope) outside that function you have to do something to make it so. For that, see the FAQ for a variety of ways: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
  2 件のコメント
Jan w
Jan w 2017 年 1 月 2 日
I'm having problems loading the *.mat file.
[folder, baseFileName] = uigetfile('Eingabedatei.mat')
folder =
Eingabedatei.mat
fullFileName =
Eingabedatei.mat\C:\Users\User1\Desktop\...
When I check the condition "exist(fullFileName, 'file')" I get a false=[0]
What is wrong?
Image Analyst
Image Analyst 2017 年 1 月 2 日
Sorry - I had it reversed. Put the filename first and the folder second:
[baseFileName, folder] = uigetfile('*.mat');
I'll correct it above too.

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

その他の回答 (1 件)

Les Beckham
Les Beckham 2016 年 12 月 17 日
編集済み: Les Beckham 2016 年 12 月 17 日
.m files are used to store Matlab code - NOT data.
You can create data in your workspace by running an m file but not by loading it.
If you are just creating a text file with manually editable data in it, you should not use the .m (or .mat) extension -- it will just cause confusion. Perhaps just use .txt (or .csv if you are using comma delimiters).
You need to be careful when creating text data files so that each row has the same number of entries or it can get tricky to read the data. You will get errors like the one in your question.
Reading a text file is best done with importdata or similar, not load.
Please clarify what type of file you are really working with (provide a short example or the real file).
  2 件のコメント
Image Analyst
Image Analyst 2016 年 12 月 18 日
In the description he said "load variables from a *.m file" and "My m file is a editable file for users" but then in his code he asked the users to load a .mat file. So who knows what the real file type is. He needs to clarify this.
The code in my solution assumed the users pick a mat file which some MATLAB program created.
If users need to edit it in notepad or whatever, then it's best to use a plain/flat text file and use importdata like Les says.
Jan w
Jan w 2017 年 1 月 2 日
Since the user uses matlab I let them edit an *.m file that creates a *.mat file that then is used for further operations.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by