How can i save Data from a Database using a Gui and for-loops / How to access a functions workspace

1 次查看(过去 30 天)
Hi dear Matlab experts, I got a problem saving data downloaded from a database using a GUI im currently working on.
The situation is: The user selects the dates and the data-names(signals) he wants and the gui saves them into Timearray and Signalarray.
What I wanna do is after hitting the gui button, collecting the signal-data of each day chosen as variables and save them to a .mat file named after that day. So if Timearray contains '20-Feb-2015' and '21-Feb-2015' and Signalarray 'Signal1' and 'Signal2', i want one 20-feb-2015.mat, with var 'Signal1' and 'Signal2' and the same with the 21-feb-2015.mat.
My Problem is, i cant save the data received from the DB into the functions WS. From where I could save the via for-loop collected variables to a mat file.
The download of the data itself works fine for me.
function pushbutton49_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton49 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TimeTableFinal;
Timearray = get(TimeTableFinal, 'Data');
Signalarray = get(handles.ChosenSignalsBox, 'String');
size_TA = size(Timearray);
size_SA = size(Signalarray);
for c1=1:size_TA(1)
BeginTicks = Timearray{c1,3};
EndTicks = Timearray{c1,4};
date_name = Timearray{c1,1};
splitDate = strsplit(date_name, {' '}); %cut date_name to create a valid filename
for c2=1:size_SA(1)
signalname = Signalarray{c2};
conn = database( *connection details* );
setdbprefs('DataReturnFormat','numeric');
sqlQuery = [*sql query string*];
curs = exec(conn, sqlQuery);
curs = fetch(curs);
signal_data = curs.Data;
assignin('base',signalname, signal_data);%renaming the the data containing variable 'signal_data' to its true name, provided by 'signalname'
% whos;
close(curs);%closing cursor and connection
close(conn);
% if c2==1
% save(splitDate{1}, signal_data)%first loop, create matfile with just recieved data
% else
% save(splitDate{1}, signal_data, '-append');%following loops, adding the just received data to already existing matfile
% end
%
end;
whos
% save(splitDate{1});
end;
At first i had the idea with
save(splitDate{1});
which doesnt work because ML then saves all variables and creates somekind of copy in the splitDate{1}-named matfile.
Then i tried
if c2==1
save(splitDate{1}, signalname)
else
save(splitDate{1}, signalname, '-append');
end
Wich should have saved the var 'signalname' to the specified matfile....but if I debug and enter a 'whos' its not even listed...so Matlab cant find it either.
Funny Thing is, the Data and the signals of the last round of the for-loop appears in my global workspace if i comment the saving parts.
Perfect would be a assign command, which assigns the variable to the functions workspace so i can use it directly.
What can i do? Thanks in advance!

采纳的回答

Mr Anderson
Mr Anderson 2015-9-16
Whoever comes across the same problem, I kinda found a solution myself. Even though most experienced programers dont like eval(), its doing the job on my problem.
Solution:
In every of the inner for-loops I reassign the data received from the DB to a variable named like specified in my Signalarray.
So it looks something like this:
evalstring = [signalname ' = signal_data;'];
eval(evalstring);
this creates a Variable named after 'signalname' in the functions Workspace which later can be saved using the if-loop ive already implemented.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by