Autorun function in guide
3 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
i have a small problem i generated an import function for the matfiles i have. When i run the function seperatly every thing is fine but i want that the function import the data when i run the main code
function importfile(Monate,Uhrzeit)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read
% Auto-generated by MATLAB on 27-Nov-2016 20:49:08
% Import the file
newData1 = load('-mat', 'Monate.mat');
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
% Import the file
newData2 = load('-mat', 'Uhrzeit.mat');
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData2);
for i = 1:length(vars)
assignin('base', vars{i}, newData2.(vars{i}));
end
2 件のコメント
Walter Roberson
2016 年 11 月 27 日
Your function is ignoring the inputs ? Should the inputs be the two file names to import?
採用された回答
Walter Roberson
2016 年 11 月 27 日
That code imports data into the base workspace. If you invoke this from a different function, you would need to fetch the result from the base workspace.
However, this code "poofs" variables into existence in the base workspace, and you are asking to instead have the variables poofed into existence in the function you are running. That is not a good idea. You should instead skip the function and use
monate_struct = load('Monate.mat');
uhrzeit_struct = load('Uhrzeit.mat');
and then reference fields of monate_struct and uhrzeit_struct . This is safer, more efficient, and easier to debug.
3 件のコメント
Walter Roberson
2016 年 11 月 29 日
load() always loads into the current workspace. If you are inside a function then it is the workspace of the function, and the values will disappear when the function returns unless you save the values.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Import and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!