Fields of Struct are not changing with functions
4 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to create a uifigure that will contain two uisliders, one that will change the frame of the image dataset that is depicted on the figure, and a uislider that will control the max range.
The problem i am facing is that , even i am calling the app struct inside the functions and I am changing the fields of frame and max range, the actual app.frames still equals to 1, even though when using the uislider for the frames, I am changing the frames. The same applies to MaxSlider; in the figure the contrast is chaning but when checking the app.displayRange field, I get the initial value I gave.
function draft_app
load("appImage.mat")
app.Figure=uifigure('Name',"A Process Try towards Improvemnet");
app.ax=uiaxes(app.Figure,'Position',[5 50 550 400]);
% ------------- Initialize Values ---------------
app.images=images;
n=size(app.images,3)
app.frame=1;
app.cmin=min(app.images(:,:,n),[],'all');
app.cmax=max(app.images(:,:,n),[],'all');
app.displayRange=[app.cmin app.cmax];
colorbar(app.ax);
app.im=imshow(app.images(:,:,app.frame),app.displayRange,'Parent',app.ax);
colormap(app.ax, jet);
% ---------------- Check Update -------------
update2(app);
%%
n=size(app.images,3);
app.frameSlider=uislider(app.Figure,'Limits',[1 n],"Position",[50 120 300 3],'Value',1);
app.frameSlider.MajorTicks = 1:1:n; % Define major ticks at intervals of 1
app.frameSlider.MinorTicks = 1:1:n; % Define minor ticks at smaller intervals, if needed
app.frameSlider.ValueChangingFcn = @(src, event) updateAppFrames(app,event.Value);
%updateAppImage(object,number,min,max)
%
app.MaxSlider=uislider(app.Figure,'Limits',[1 app.cmax],"Position",[50 100 300 3],'Value',app.cmax);
app.MaxSlider.MajorTicks = []; % Define major ticks at intervals of 1
app.MaxSlider.MinorTicks = []; % Define minor ticks at smaller intervals, if needed
app.MaxSlider.ValueChangingFcn = @(src, event) updateAppMax(app,event.Value);
end
function updateAppMax(app,maximum)
app.cmax=maximum;
app.displayRange=[app.cmin app.cmax];
update2(app);
end
function update2(app)
set(app.ax,'CLim',app.displayRange);
fprintf('The range is %1.2f - %1.2f\n',app.displayRange(1),app.displayRange(2));
set(app.im,'CData',app.images(:,:,app.frame));
end
function updateAppFrames(app,frame)
app.frame=round(frame);
% app.cmin=min(app.images(:,:,app.frame),[],'all');
% app.cmax=max(app.images(:,:,app.frame),[],'all');
% app.displayRange=[app.cmin app.cmax];
update2(app);
end
0 个评论
采纳的回答
Stephen23
2023-11-29
编辑:Stephen23
2023-11-29
" even i am calling the app struct inside the functions and I am changing the fields of frame and max range, the actual app.frames still equals to 1"
If you simply pass a structure to a local function then any changes to the structure within that local function will remain within that local function. The changes are discarded when the function returns.
The MATLAB documentation explains how to pass data between callback functions:
A simple and intuitive way to pass the data is to use nested functions. I modified your function to use nested functions, it works without error. I had to comment-out one command because you referred to a property that does not exist.
function draft_app
S = load("appImage.mat"); % much better to LOAD into an output variable.
S.Figure = uifigure('Name',"A Process Try towards Improvement");
S.ax = uiaxes(S.Figure,'Position',[5 50 550 400]);
%
%% Initialize Values
%
n = size(S.images,3);
S.frame = 1;
S.cmin = min(S.images(:,:,n),[],'all');
S.cmax = max(S.images(:,:,n),[],'all');
S.displayRange = [S.cmin,S.cmax];
colorbar(S.ax);
S.im = imshow(S.images(:,:,S.frame),S.displayRange,'Parent',S.ax);
colormap(S.ax, parula);
%
%% Check Update
%
update2();
%
n = size(S.images,3);
S.frameSlider = uislider(S.Figure,'Limits',[1,n],"Position",[50,120,300,3],'Value',1);
S.frameSlider.MajorTicks = 1:1:n; % Define major ticks at intervals of 1
S.frameSlider.MinorTicks = 1:1:n; % Define minor ticks at smaller intervals, if needed
S.frameSlider.ValueChangingFcn = @updateAppFrames;
%updateAppImage(object,number,min,max)
%
S.MaxSlider = uislider(S.Figure,'Limits',[1,S.cmax],"Position",[50,100,300,3],'Value',S.cmax);
S.MaxSlider.MajorTicks = []; % Define major ticks at intervals of 1
S.MaxSlider.MinorTicks = []; % Define minor ticks at smaller intervals, if needed
S.MaxSlider.ValueChangingFcn = @updateAppMax;
%
%% Nested Functions
%
function updateAppMax(~,event)
%S.cmax = event.maximum; % !!!! MAXIMUM is not a property of this EVENT !!!!
S.displayRange=[S.cmin,S.cmax];
update2();
end
%
function update2()
set(S.ax,'CLim',S.displayRange);
fprintf('The range is %1.2f - %1.2f\n',S.displayRange(1),S.displayRange(2));
set(S.im,'CData',S.images(:,:,S.frame));
end
%
function updateAppFrames(~,event)
S.frame = round(event.Value);
% S.cmin=min(S.images(:,:,S.frame),[],'all');
% S.cmax=max(S.images(:,:,S.frame),[],'all');
% S.displayRange=[S.cmin,S.cmax];
update2();
end
%
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!