How can share the handles variable between two skripts?

My question ist:
1) i have created a panel with text in a skript
str_velocity = "%.2f km/h";
vxvRef= 0;
handles.str_velocity = sprintf(str_velocity, vxvRef);
if ~isempty(vxvRef)
uicontrol(handles.vehicleData, 'Style','text','Position', [148 -12 87 100], 'String',handles.str_velocity,'FontSize',11);
end
2) In another skript i have:
vxvRef= 3;
% try to acces the handles.str_velocity and update the new vxvRef into handles.str_velocity to show this in the panel
set(handles.vehicleData,'String',vxRef)
it showed me an error. How can I share the handles variable between two skripts and is my approach right ? Thanks

댓글 수: 4

Share your entire code, and the error message so that we can help you.
%%%In the first Skript:
handles.vehicleData = uipanel(...
'Title','Vehicle Data',...
'FontSize',12,...
'Units','pixels',...
'Position',[1337 10 290 130]);
label_vehicleData = {'Ego velocity: ', 'Ego acceleration: ', 'Ground classification: '};
uicontrol(handles.vehicleData, 'Style','Text','String', label_vehicleData(1), 'FontSize',11,... 'Position', [-48 -5 200 95])
uicontrol(handles.vehicleData, 'Style','Text','String', label_vehicleData(2), 'FontSize',11,... 'Position', [-29 -35 194 98])
uicontrol(handles.vehicleData, 'Style','Text','String', label_vehicleData(3), 'FontSize',11,... 'Position', [-18 -63 194 98])
% velocity
str_velocity = "%.2f km/h";
vxvRef= 0;
handles.str_velocity = sprintf(str_velocity, vxvRef);
if ~isempty(vxvRef)
uicontrol(handles.vehicleData, 'Style','text','Position', [148 -12 87 100],... 'String',handles.str_velocity,'FontSize',11);
end
%%%In the second Skript:
vxvRef = handles.data.dat(1).evi_General.vxvRef(cycle)/normVelocity;
vxvRef = round(3.6*vxvRef*10)/10;
str_velocity = "%.2f km/h";
a = sprintf(str_velocity, vxvRef);
set(handles.str_velocity ,'String', a);
Error:
Reference to non-existent field 'str_velocity'.
Adam
Adam 2019년 2월 5일
편집: Adam 2019년 2월 5일
If you run multiple scripts in sequence from the same workspace and subsequent scripts after the first don't have the deadly
clear all
or its cohorts then you will have access in subsequent scripts to anything you created in previous ones.
I would not recommend this method of programming though. You should use functions to encapsulate code and input and output arguments as appropriate to allow data to pass into and out of the workspace of each function.
That is also a good methode. thanks Adam!

댓글을 달려면 로그인하십시오.

 채택된 답변

Luna
Luna 2019년 2월 5일
편집: Luna 2019년 2월 5일
If they are scripts, they should be created as methods of a class which is abstracted from handle superclass.
classdef MyClass < handle
properties
prop1
prop2
prop3
end
methods
function obj = MyClass
obj.prop1 = uicontrol (.. 'Style','text',..);
obj.prop2 = uicontrol (..'Style','pushbutton','Callback',@obj.ButtonPushedCallback);
obj.prop3 = 500 % some velocity or other values
end
function textChange(obj,source,event)
% obj is MyClass object
% source is text object
% event is the event indicates any action taken
obj.prop1.String = 'NewString';
% You can also change text element string with using source object
% source.String = 'NewString';
end
function ButtonPushedCallback(obj,source,event)
% obj is MyClass object
% source is pushbutton object
% event is the event indicates that button pressed
obj.textChange; % calls the text change function above
end
end
end
% You can reach all properties in all methods defined in this class with obj.property notation.

댓글 수: 1

Mai Le Thai
Mai Le Thai 2019년 2월 5일
편집: Mai Le Thai 2019년 2월 5일
Thanks Luna. I am going to try your methode

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

제품

릴리스

R2018b

태그

질문:

2019년 2월 5일

편집:

2019년 2월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by