The figure is filled and the system RAM does not respond - a problem in optimizing the code
1 view (last 30 days)
Show older comments
Hello, the code runs correctly, but there is a problem, that is, I am no longer able to optimize the code, and I am tired of this work.
please help
Code description:
At first, 300 files are read in mat format, and then they must be saved one by one as photo files in jpg format after executing the formula.
which gets stuck in the 150 code loop and is unable to continue and MATLAB closes completely
please help please...
clc
clear
close all
Rf1=[0.001 10 50 100 200];
L_line1=[4 50 100 140 150 196];
tetta1=[10 -15 20 30 40];
%% read data
for i=1:150
if i<=30
f1(i)=Rf1(1);
elseif i<=60
f1(i)=Rf1(2);
elseif i<=90
f1(i)=Rf1(3);
elseif i<=120
f1(i)=Rf1(4);
else
f1(i)=Rf1(4);
end
end
for i=1:30
if i<=5
f2(i)=L_line1(1);
elseif i<=10
f2(i)=L_line1(2);
elseif i<=15
f2(i)=L_line1(3);
elseif i<=20
f2(i)=L_line1(4);
elseif i<=25
f2(i)=L_line1(5);
else
f2(i)=L_line1(6);
end
end
for i=1:150
f1_1=num2str(f1(i));
prt1=strcat(f1_1,'_');
for i2=1:30
f2_2=num2str(f2(i2));
prt2=strcat(f2_2,'_');
for i3=1:5
f3_3=num2str(tetta1(i3));
prt3=strcat(f3_3,'\');
print=strcat('C:\Users\MASHHADSERVICE\Downloads\data\SPS\S_',prt1,prt2,prt3,'Fi_',prt1,prt2,f3_3,'.mat');
load(print);
If=cell2mat(Y);
If_1=If(1999:2401);
eval(['If', num2str(i) , ' =If_1;']);
print1=strcat('C:\Users\MASHHADSERVICE\Downloads\data\SPS\S_',prt1,prt2,prt3,'Si_',prt1,prt2,f3_3,'.mat');
load(print1);
Is=cell2mat(Y);
Is_1=Is(1999:2401);
eval(['Is', num2str(i) , ' =Is_1;']);
end
end
end
load('C:\Users\MASHHADSERVICE\Downloads\data\SPS\S_0.001_4_10\time.mat');
t1=cell2mat(Y);
time=t1(2001:2400);
%% fdeo
for i=1:150
x1=eval(['Is', num2str(i)]);
x2=hilbert(x1);
x3=x1+x2;
eval(['Is_f', num2str(i) , ' =x3;']);
y1=eval(['If', num2str(i)]);
y2=hilbert(y1);
y3=y1+y2;
eval(['If_f', num2str(i) , ' =y3;']);
end
for i=1:150
f1_1=num2str(f1(i));
prt1=strcat(f1_1,'_');
%% fdeo
nfdo_i=eval(['Is_f', num2str(i)]);
nfdo_f=eval(['If_f', num2str(i)]);
for i2=1:400
fdeo_power(i2)=((nfdo_i(i2+1))^2) + (nfdo_i(i2)*nfdo_i(i2+2));
fdeo_fault(i2)=((nfdo_f(i2+1))^2) + (nfdo_f(i2)*nfdo_f(i2+2));
end
%% nfdeo
fdeo_mean_power=mean(fdeo_power,'all');
fdeo_mean_fault=mean(fdeo_fault,'all');
for i3=1:400
nfdeo_power(i3)=((fdeo_power(i3)-fdeo_mean_power)/fdeo_mean_power);
nfdeo_fault(i3)=((fdeo_fault(i3)-fdeo_mean_fault)/fdeo_mean_fault);
end
figure(i)
plot(time,nfdeo_power)
figure(i+1)
plot(time,nfdeo_fault)
end
for i5=1:150
for i4=1:30
f2_2=num2str(f2(i4));
prt2=strcat(f2_2,'_');
for i3=1:5
f3_3=num2str(tetta1(i3));
save_as_power=strcat('nfdo_P_',prt1,prt2,f3_3,'.png');
saveas(figure(i),save_as_power,'png');
save_as_fault=strcat('nfdo_f_',prt1,prt2,f3_3,'.png');
saveas(figure(i+1),save_as_fault,'png');
end
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1427153/image.png)
2 Comments
Image Analyst
on 5 Jul 2023
Edited: Image Analyst
on 5 Jul 2023
Please edit your post to format it as code, add comments and attach needed .mat files and your .m file so we can understand and run your code.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Answers (1)
Sivsankar
on 6 Jul 2023
It seems like you're facing some issues with optimizing your code and it gets stuck in a loop. I'll try to help you with that. Here are a few suggestions to optimize your code:
- Preallocate Arrays: Before the loop, preallocate arrays f1, f2, If, Is, If_f, and Is_f using the zeros function. This will improve the performance by avoiding dynamic resizing of arrays inside the loop.
- Use Vectorization: Instead of using nested loops to assign values to f1, f2, If, and Is, you can use vectorization and indexing operations to assign values directly. For example:
f1 = zeros(1, 150);
f1(1:30) = Rf1(1);
f1(31:60) = Rf1(2);
% ... and so on
Use sprintf Instead of num2str: Instead of using num2str to convert numbers to strings, you can use sprintf which provides more control over formatting. For example:
f1_1 = sprintf('%.3f', f1(i));
Avoid Using eval: The use of eval is generally discouraged as it can make the code harder to read and debug. Instead, consider using dynamic field names or cell arrays to store your variables.
Reduce Redundant Code: There are some parts of your code that can be simplified and reduced to avoid unnecessary repetitions. For example, the code for saving figures can be moved inside the loop where the figures are created.
Applying these optimizations should help improve the performance and readability of your code. Why dont you try these out
0 Comments
See Also
Categories
Find more on Convert Image Type 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!