Reuse the variable created in a loop in a function

Hi,
How can I reuse the the variable created in a loop in a function? When I do debug, I found that the variable was not saved to workspace after the loop.
The variable can only be defined in the loop but not before the loop. Is there any way I can save the variable that is created in the loop so that I can reuse it?
Thank you!

댓글 수: 3

Please post your code. Without seeing it it is hard to understand, what you explain.
Actually it works:
for k = 1:10
y = 2;
end
disp(y)
Or
z = zeros(1, 10);
for k = 1:10
z(k) = rand;
end
disp(z)
YingDu's "Answer" moved here:
%%function that supposed to be called by GUI
%function [num_cells, cell_x, cell_y, show_FOV, peak_val, config_pv_SNR, img_stack] = cell_diagnose_v7(readFileDir, save_file_Dir, keyword, filetype, crop_radius, ...
%config_names, config_color_value, config_gain_value, bit_depth, config_gain_value_forDisplay, config_SNRthresholds)
for config_idx = 1:num_configs
m_read_file = [readFileDir filesep keyword '*' config_names{config_idx} filetype];
m_allfiles = dir(m_read_file); % we only expect one file for this configuration
m_img = imread([readFileDir filesep m_allfiles(1).name]);
img_stack = zeros(num_row, num_col, num_configs);
img_stack(:,:,config_idx) = m_img; % img_stack
end
imagesc(img_stack(:,:,1)) % why it is not saved, I want to use img_stack in the following function.
YingDu's "Answer" moved here:

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

답변 (2개)

Jan
Jan 2018년 2월 25일
편집: Jan 2018년 2월 25일
You overwrite the 3D array in each iteration, Create the array to be filled before the loop:
img_stack = zeros(num_row, num_col, num_configs); % *Before* the loop
for config_idx = 1:num_configs
m_read_file = [readFileDir filesep keyword '*' config_names{config_idx} filetype];
m_allfiles = dir(m_read_file); % we only expect one file for this configuration
m_img = imread([readFileDir filesep m_allfiles(1).name]);
% No - not here: [img_stack = zeros(num_row, num_col, num_configs);]
img_stack(:, :, config_idx) = m_img; % img_stack
end
With your code, img_stack was set to zeros in each iteration, such that only the last img_stack(:, :, num_configs) was stored after the loop.
I gave you an example in my comments already:
z = zeros(1, 10);
for k = 1:10
z(k) = rand;
end
disp(z)
Compare this with your code:
for k = 1:10
z = zeros(1, 10);
z(k) = rand;
end
disp(z)
Ying Du
Ying Du 2018년 2월 25일

0 개 추천

Hi,
I have attached my sample code. The part of function shown in the screenshot is supposed to be called by a GUI.
Best, Ying

댓글 수: 2

Jan
Jan 2018년 2월 25일
Please insert information needed to understand the question by editing the question. There is the place, where readers expect to find all details. At least use the comments. But do not post details in the section for answers to avoid a confusion. Thanks.
Hi Ying Du
this is John BG jgb2012@sky.com
You have to
1.- define the variable collecting results BEFORE running the function, and
2.- make sure you pull the result out of the function
1.-
this
close all;clear all;clc
y0=1
function test123(y0)
for k=1:1:10
y=y+k
end
y_result=y
end
and this
close all;clear all;clc
y0=1
function y_result=test123(y0)
for k=1:1:10
y=y+k
end
neither see the resulting value of y, despite having declared y_result as output of the support function
The problem gets solved with the following:
close all;clear all;clc
y0=1
y_result=0;
function y_result=test123(y0)
for k=1:1:10
y=y+k
end
y_result=y
end
2.-
the variables that you create inside the loop, inside the function
m_read_file
m_allfiles
m_img
are not included in the outputs of the function
[num_cells, cell_x, cell_y, show_FOV, peak_val, config_pv_SNR, img_stack]
If you declare
m_read_file
m_allfiles
m_img
In the output of the function then such variables will be available once the for loop and the function completed.
John BG

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

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 2월 24일

댓글:

2018년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by