Autorun function in guide

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 个评论

Your function is ignoring the inputs ? Should the inputs be the two file names to import?
i get the result i wanted i used run importfile in the maincoude opening function so the imporftile.m get triggert and import my data i need:)

请先登录,再进行评论。

 采纳的回答

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 个评论

So when i use this i can use every data inside the matfiles? Example: Every month is stored in this Matfile seperatly Januar Februar and so on and i want to get data from januar only from file monate.mat :)
for H = Januar(:,:)
E=Pmpp* f *H *PR * Nw *W
When I go into the debugg modus and implement this in the openingfcn I can see the struct will be loaded but if i set a next debug point to a another point i see the data will be lost? Or the data still exist and the debug modus only load the data which will be generated/used in this function?
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.
Thank you for your big help. I have done everything without your last tip (just changed the position of some variables). It seems not so good but for the first result the programm do what it should do. Now i will work on it to make a clean code! A Big THANKS!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by