Transfer data from callback workspace to base workspace

16 visualizaciones (últimos 30 días)
Bart
Bart el 20 de Mzo. de 2013
Respondida: Oussama Islamo el 23 de Abr. de 2019
Hello everybody,
First of all, I'm a matlab Noob, so if I ask weird questions, sorry about that. I've asked some questions before here, and every time I was getting a littlebit further, so thank you for that. I've looked around on the internet to find an answer for my problem, and I can find some information, but not specific an answer for my problem.
So, my new problem:
I've made an interface with 3 dropdown menu's. On the basis of choices which are made in these dropdown menu's, with the push of a button, a few other m-files has to run. These m-files are processing data from a excel file to matlab. The running of the matlab files, when pushing on the button is working, but I cannot find the data of the excel file. I know that the data (which is a lot!) is somewhere "behind" the pushbutton. Now, with the use of another button in the same interface, the data which comes from the Excel file, has to be transfered to the base workspace. Also, I know that this has something to do with evallin or assignin. But I don't know how I can use this, if I have multiple m-files which also process a lot of data.
I hope that I have myself clear, do to my problem. If something is not clear, I will try to specify it further.
Thank you in advance!
Below, my matlab code of the GUI.
function varargout = InputV1(varargin)
% INPUTV1 MATLAB code for InputV1.fig
% INPUTV1, by itself, creates a new INPUTV1 or raises the existing
% singleton*.
%
% H = INPUTV1 returns the handle to a new INPUTV1 or the handle to
% the existing singleton*.
%
% INPUTV1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in INPUTV1.M with the given input arguments.
%
% INPUTV1('Property','Value',...) creates a new INPUTV1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before InputV1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to InputV1_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help InputV1
% Last Modified by GUIDE v2.5 13-Mar-2013 15:39:57
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @InputV1_OpeningFcn, ...
'gui_OutputFcn', @InputV1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before InputV1 is made visible.
function InputV1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to InputV1 (see VARARGIN)
% Choose default command line output for InputV1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes InputV1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = InputV1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu2.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Get values from POPUPMENU1 for TLB configuration
switch get(handles.popupmenu1,'Value')
case 2
PI_342W2_TLB_Jib_FM
case 3
PI_342W2_TLB_Jib_FU
case 4
PI_342W2_TLB_Module_FM
case 5
PI_342W2_TLB_Module_FU
case 6
PI_342W2_TLB_ModuleNOCOB_FM
case 7
PI_342W2_TLB_ModuleNOCOB_FU
otherwise
end
%Get values from POPUPMENU2 for JIB configuration
switch get(handles.popupmenu2,'Value')
case 2
PI_342W3_Jib_Module
case 3
PI_342W3_Jib_Upending
otherwise
end
%run remaining m-files
MISC
PI_341W1_120_Module_handling
PI_343W4_Auxiliary_hoist
PI_344W6_Derrick
PI_344W7_COB
%PI_344W8_Upend
PI_348k0_Controls
Vessel_data
msgbox 'Data has been processed'
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
msgbox 'Data has been stored in the base workspace'

Respuestas (3)

Bart
Bart el 3 de Abr. de 2013
Hello,
Because of the fact that I didn't recieve other answers, I tried it in a different way, because I couldn't find out how to go further with the answer from Vishal Rane.
At the end of
function pushbutton2_Callback(hObject, eventdata, handles)
I put the code:
save guioutput
filename = uigetfile('guioutput.mat');
command = sprintf('load(''%s'')', filename);
evalin('base', command);
Now, I get a .mat file, which I open with the code above. But I have to press OK, before it opens. This is not what I want, because it is possible that this code has to run for example a 1000 times. So pressing 1000 times on OK isn't an option.
My question is if someone can explain how to get further with the answer Vishal Rane gave, or that someone can explain me how to get further with the code above.
Thank you in advance! Bart
  2 comentarios
Bart
Bart el 3 de Abr. de 2013
I want to inform you that I worked it out.
It was easier than I thought, but with no experience with matlab, harder to figur out. I deleted the above and only used the next code:
save
evalin('base','load')
Now, all the data is in the base workspace. The problem was that I saved it in guioutput.mat. And than, the evalin code with the code:
evalin('base','guioutput')
Gave an error. But now, it works for me. Thank you for answering or reading my question.
Georges Kanaan
Georges Kanaan el 24 de Mzo. de 2015
that was helpful for me :) thank you Bart

Iniciar sesión para comentar.


Vishal Rane
Vishal Rane el 20 de Mzo. de 2013
You could modify these m-files (that are processing data from the excel file) to dump the processed data directly to the base WS. This would eliminate the need of an additional button press.
Say these mfiles read a column of data from your excel file and store it in a local variable ' ProcessedData '
To dump that data into the base, you could use :
assignin('base','ProcessedData1',ProcessedData)
See assignin for more info
  1 comentario
Bart
Bart el 20 de Mzo. de 2013
Editada: Bart el 20 de Mzo. de 2013
Thank you for your advice.
If I understand you correctly, I have to modify my m-files which are retrieving data from my Excel file. The way that I retrieve this data is as follows:
if strcmp(deblank(tekst(i,1)),'Module.mass')
Module.mass = num(i,1);
else
disp('Module.mass is not specified correctly')
end
And offcourse, a lot more of these. When I run this by hand, and I typ Module.mass in the command window, I will recieve a number. That is what I also need after pressing the push button in the interface.
So to summarize: Each m-file that retrieves data from the Excel file, has to be adjusted in a local variable, such as "ProcessedData". Then, in the callback of the push button I have to put something like
assignin('base','ProcessedData1',ProcessedData)
Am I correct?
If so, how do I store all the data per m-file in a local variable?
And another question: Do I get a local variable per m-file? And do I have to "dump" every variable into the base? For example:
assignin('base','ProcessedData1',ProcessedData)
assignin('base','ProcessedData2',ProcessedData)
assignin('base','ProcessedData3',ProcessedData)
etc.
Thank you very much for your help. And sorry in case I'm asking to easy questions. But i'm learning!

Iniciar sesión para comentar.


Oussama Islamo
Oussama Islamo el 23 de Abr. de 2019
how can i load a .mat file from a push botton and save this file in work space,, i have the import code of .mat file:
% --- Executes on button press in push1.
function push1_Callback(hObject, eventdata, handles)
% hObject handle to push1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load('fname.mat')
[fname,pname] = uigetfile('*.mat');
but I need the instruction how to locate my data in workspace...

Categorías

Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by