The values from the for loop won't store on the array
    4 views (last 30 days)
  
       Show older comments
    
    Imanol Fernandez de arroyabe Zabala
 on 26 Oct 2023
  
    
    
    
    
    Answered: Sulaymon Eshkabilov
      
 on 26 Oct 2023
            When running this piece of code, pendiente(i) will display the values, but when I exit the loop the array shows up as empty. What am I doing wrong?
for i = 1 : length(epsilon_l_embuticion)-2
    pendiente(i) = (sigma_mpa_embuticion(i+1)-sigma_mpa_embuticion(i))/(epsilon_l_embuticion(i+1)-epsilon_l_embuticion(i));
    pendiente(i)
end
pendiente
5 Comments
  Torsten
      
      
 on 26 Oct 2023
				Except for some Inf values in "pendiente", everything works out fine (see above).
  Dyuman Joshi
      
      
 on 26 Oct 2023
				
      Edited: Dyuman Joshi
      
      
 on 26 Oct 2023
  
			I have ran your code here, just to show that the result is not empty, but instead of importdata(), use readmatrix or readtable or readcell, as they are more robust functions.
There is a big disparity in the (absolute) values of output, from the order of 10^2 to 10^13, thus smaler values might "appear" to be zero or "empty", but they are not.
B = importdata("Acero_embuticion.txt")
sigma_mpa_embuticion = B.data(:, 1);
epsilon_l_embuticion = B.data(:, 2);
epsilon_t_embuticion = B.data(:, 3);
for i = 1 : length(epsilon_l_embuticion)-2
    pendiente(i) = (sigma_mpa_embuticion(i+1)-sigma_mpa_embuticion(i))/(epsilon_l_embuticion(i+1)-epsilon_l_embuticion(i));
    %pendiente(i)
end
format shortg
disp(pendiente.')
Answers (1)
  Sulaymon Eshkabilov
      
 on 26 Oct 2023
        You can display/write out the calculated data, and store or write to an external files as well. See how it can be done:
A = readmatrix("Fundicion_gris.txt");
B = readmatrix("Acero_embuticion.txt");
sigma_mpa_embuticion = B(:, 1);
epsilon_l_embuticion = B(:, 2);
epsilon_t_embuticion = B(:, 3);
for i = 1 : length(epsilon_l_embuticion)-2
    pendiente(i) = (sigma_mpa_embuticion(i+1)-sigma_mpa_embuticion(i))/(epsilon_l_embuticion(i+1)-epsilon_l_embuticion(i));
end
% The calculated data is stored/written into an external file called: Out_pendiente.txt
writematrix(transpose(pendiente), 'Out_pendiente.txt')
fprintf('ALL calculated data = pendiente : \n');
fprintf('%3.5f \n', transpose(pendiente))
% An alternative way of storing the data is to use save() 
save('Out_pendiente2.mat', pendiente)
0 Comments
See Also
Categories
				Find more on Data Type Conversion in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



