save the loop data in a if else statement

I have an if statement
for ind = 1:MAX_ITER
if(mod(ind, 1500) == 0)
h = findobj(gca,'Type','line');
x=get(h,'Xdata');
y=get(h,'Ydata');
z=[x,y];
H_XX = z{2,1};
H_YY = z{2,2};
filename=['test',num2str(ind),'.mat'];
save(filename)
end
end
How can i save the 'H_XX' and 'H_YY' of the entire loop in a single file ?

 Respuesta aceptada

KSSV
KSSV el 3 de Abr. de 2018
count = 0 ;
iwant = cell([],1) ;
for ind = 1:MAX_ITER
if(mod(ind, 1500) == 0)
h = findobj(gca,'Type','line');
x=get(h,'Xdata');
y=get(h,'Ydata');
z=[x,y];
H_XX = z{2,1};
H_YY = z{2,2};
count = count+1 ;
iwant{count,1} = [H_XX H_YY] ;
end
end
iwant = cell2mat(iwant) ;
iwant gives you data when the loop goes inside if. You can save it into a file you want.

3 comentarios

SUSHMA MB
SUSHMA MB el 3 de Abr. de 2018
thanks for the code...but i am not able to view the file 'iwant'
KSSV
KSSV el 4 de Abr. de 2018
iwant is a variable...not a file. YOu can save it using save.
SUSHMA MB
SUSHMA MB el 4 de Abr. de 2018
Thank you for the answer....

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 3 de Abr. de 2018
Editada: Stephen23 el 3 de Abr. de 2018
It is simpler to avoid generating too many values (and then using mod to select which values you want to use) because then you can trivially preallocate an output array of the correct size:
vec = 1500:1500:MAX_ITER;
out = cell(numel(vec),2);
for k = 1:numel(vec)
...
out(k,:) = [x,y];
end
save('test.mat','out')
This trivially avoids expanding the output array on each iteration:

3 comentarios

SUSHMA MB
SUSHMA MB el 3 de Abr. de 2018
Thank you. But its a part of an entire code....i just want to save the 'H_XX' 'H_YY' in a single file.
Stephen23
Stephen23 el 4 de Abr. de 2018
@SUSHMA MB: sure, that is what my code shows you.
SUSHMA MB
SUSHMA MB el 4 de Abr. de 2018
Thank you for the answer....

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 3 de Abr. de 2018

Comentada:

el 4 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by