Hello,
I've created a GUI in GUIDE and on the push of a button, I want to transfer data saved within the MATLAB 'base' workspace to an Excel file. I'm aware I can do this from the command window using the following code:
xlswrite('Vehicle_Data_', velocity_data, 'Sheet 1', 'A1')
However, how do I implement this within my pushbutton callback shown below:
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Ideally, an example would be a massive help!
Thanks,
Craig

1 commentaire

Stephen23
Stephen23 le 17 Août 2018
Modifié(e) : Stephen23 le 17 Août 2018
"I want to transfer data saved within the MATLAB 'base' workspace to an Excel file"
Magically grabbing data from another workspace and magically accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code which is hard to debug. The best solution is to pass that data properly as input/output arguments, exactly as the MATLAB documentation recommends:

Connectez-vous pour commenter.

 Réponse acceptée

Adam Danz
Adam Danz le 17 Août 2018

0 votes

From your bushbutton callback you can evaluate the expression from the base workspace. If you get stuck, follow-up and I can help.

6 commentaires

Hi Adam, Thanks for your answer. Ideally, i would like to push a button on my GUI, and then it'll save data which was stored from a simulink simulation to an Excel file.
My simulation transfers this data to the workspace, however i just need to find a way of using
xlswrite
to then send this data to Excel.
Yep, that's what I understood. evalin() is a solution.
One option is to get the needed data from the workspace and transfer it to your callback function using evalin. Then you can call xlswrite() from the callback function.
Here's an example of getting the variable named 'myVar' from the workspace to the callback function. You would grab all of your needed variables and then call xlswrite().
% (from callback function)
myVar = evalin('base', 'myVar');
The second option is to call xlswrite() from the base workspace (using evalin).
Here's how that might look.
% (from callback function)
evalString = 'xlswrite(''Vehicle_Data_'', velocity_data, ''Sheet 1'', ''A1'')';
evalin('base', evalString)
Craig Saunders
Craig Saunders le 17 Août 2018
Absolutely spot on, Adam. Thank you.
Craig Saunders
Craig Saunders le 17 Août 2018
On a quick note, if i wanted to export multiple data sets such as 'force_data', 'distance_data' ... etc - how would this be done?
Thank you.
Adam Danz
Adam Danz le 17 Août 2018
Modifié(e) : Adam Danz le 17 Août 2018
You could do it the same way you did with the velocity_data. You could do that in a loop and write each variable to a new sheet... something like this
Not tested
exportVars = {'velocity_data', 'force_data', 'distance_data'};
for i = 1:length(exportvars)
evalString = sprintf('''xlswrite(''Vehicle_Data'', %s, %s, ''A1''', exportVars{i}, sprintf('''Sheet %d''', i))
myVar = evalin('base', evalString);
end
Craig Saunders
Craig Saunders le 17 Août 2018
Excellent, just what I needed - thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by