Data is not storage when running loop
109 views (last 30 days)
Show older comments
A-Rod
on 12 Sep 2024 at 23:23
Commented: A-Rod
on 17 Sep 2024 at 12:22
hello community
please help me to understand why data does not show when I run a loop:
I have 2 files: @Data01.mat and @Data05.mat I want to extrac data points when conditions are met'
if I run my script with Data1 all the parameters are there:
figure;plot (Epm_nEngallc0_4, IKCtl_au16_0allc, '.');
but if I run my script with Data1&2 the output is different and I get less data points
I would expect to have all data poins from @Data01.mat plus @Data05.mat
please help me to see what am I doing wrong.
clear all; %
clc; %
fprintf('select folder containing data files\n');
currentpath=pwd; %%
datapath=uigetdir('','Select Data Folder'); %%
if (datapath == 0)
fprintf('Data directory was not selected...script will be terminated\n\n');
return
end
prompt = 'Do you want to export data as .xlsx?';
save_data_excel = questdlg(prompt,'Save','Yes','No','Yes');
cd(datapath); %%
files=dir('@*.mat'); %%
num_files=length(files(:,1)); %%
for i=1:num_files
fprintf('Progress: %d/%d\n\n',i, num_files)
load(files(i,1).name)
for j=1:length(time)
if Epm_nEng(j) > 0 && IKCtl_DiagRefLvlMinErr_au16__ls_1_rs_(j) > 0.165 && IKCtl_DiagRefLvlMinErr_au16__ls_3_rs_(j) > 0.165%% sensor signal
Epm_nEngallc1_3(j,1) = Epm_nEng(j); %% get engine speed if: engine speed is higher than 0 and if sensor signal > 0.165
end
if Epm_nEng(j) > 0 && IKCtl_DiagRefLvlMinErr_au16__ls_2_rs_(j) > 0.165 && IKCtl_DiagRefLvlMinErr_au16__ls_5_rs_(j) > 0.165
Epm_nEngallc2_5(j,1) = Epm_nEng(j);
end
if Epm_nEng(j) > 0 && IKCtl_DiagRefLvlMinErr_au16__ls_0_rs_(j) > 0.165 && IKCtl_DiagRefLvlMinErr_au16__ls_4_rs_(j) > 0.165
Epm_nEngallc0_4(j,1) = Epm_nEng(j);
end
if Epm_nEng(j) > 0 && IKCtl_DiagRefLvlMinErr_au16__ls_1_rs_(j) > 0.165 && IKCtl_DiagRefLvlMinErr_au16__ls_3_rs_(j) > 0.165
IKCtl_au16_1allc(j,1)= IKCtl_DiagRefLvlMinErr_au16__ls_1_rs_(j); %% get sensor signal if: engine speed is higher than 0 and if sensor signal > 0.165
IKCtl_au16_3allc(j,1)= IKCtl_DiagRefLvlMinErr_au16__ls_3_rs_(j);
end
if Epm_nEng(j) > 0 && IKCtl_DiagRefLvlMinErr_au16__ls_2_rs_(j) > 0.165 && IKCtl_DiagRefLvlMinErr_au16__ls_5_rs_(j) > 0.165
IKCtl_au16_2allc(j,1)= IKCtl_DiagRefLvlMinErr_au16__ls_2_rs_(j);
IKCtl_au16_5allc(j,1)= IKCtl_DiagRefLvlMinErr_au16__ls_5_rs_(j);
end
if Epm_nEng(j) > 0 && IKCtl_DiagRefLvlMinErr_au16__ls_0_rs_(j) > 0.165 && IKCtl_DiagRefLvlMinErr_au16__ls_4_rs_(j) > 0.165
IKCtl_au16_0allc(j,1)= IKCtl_DiagRefLvlMinErr_au16__ls_0_rs_(j);
IKCtl_au16_4allc(j,1)= IKCtl_DiagRefLvlMinErr_au16__ls_4_rs_(j);
end
end
end
3 Comments
Image Analyst
on 13 Sep 2024 at 3:43
Edited: Image Analyst
on 13 Sep 2024 at 3:46
Looks like you blew right past the posting guidelines. If you have any more questions, then attach your 3 data mat files (data01, data02 and data05) with the paperclip icon after you read this:
It's not obvious from your two plots that data05 has fewer points in it than data01 combined with data02.
And how are you plotting? I don't see plot() or scatter() in your code.
And put in some comments! We have no idea what all those variables mean. Help us to help you.
And don't use cd():
Use fullfile() instead.
Accepted Answer
Epsilon
on 17 Sep 2024 at 10:06
Edited: Epsilon
on 17 Sep 2024 at 10:16
Hi A-Rod,
On running the original code with both the data files, the number of data points generated is dependent on the highest value of ‘length(time)’. If you load onlyData01.mat, you get 5092 data points, and withData05.mat, you get 6167. However, when both files are used, the total number of data points ends up being 6167. This happens because the first 5092 datapoints are overwritten due to the same index(j) being passed in the second iteration.
Also when using just the Data01.mat file the output variables have a wider range. When both the files are used the higher values of the variables are overwritten based on the If statements, and then the variables have a narrower range. Thus, the plot using both the files has lower values on the axes. The no. of datapoints is still higher.
If you want to add all the datapoints consider appending the variable values after the existing index and not overwriting them, it can look something like this:
% Initialize arrays outside the loops to store results
Epm_nEngallc1_3 = [];
Epm_nEngallc2_5 = [];
Epm_nEngallc0_4 = [];
IKCtl_au16_1allc = [];
IKCtl_au16_3allc = [];
IKCtl_au16_2allc = [];
IKCtl_au16_5allc = [];
IKCtl_au16_0allc = [];
IKCtl_au16_4allc = [];
for i = 1:num_files
fprintf('Progress: %d/%d\n\n', i, num_files);
load(files(i, 1).name);
for j = 1:length(time)
if Epm_nEng(j) > 0 && IKCtl_DiagRefLvlMinErr_au16__ls_1_rs_(j) > 0.165 && IKCtl_DiagRefLvlMinErr_au16__ls_3_rs_(j) > 0.165
Epm_nEngallc1_3 = [Epm_nEngallc1_3; Epm_nEng(j)];
IKCtl_au16_1allc = [IKCtl_au16_1allc; IKCtl_DiagRefLvlMinErr_au16__ls_1_rs_(j)];
IKCtl_au16_3allc = [IKCtl_au16_3allc; IKCtl_DiagRefLvlMinErr_au16__ls_3_rs_(j)];
end
if Epm_nEng(j) > 0 && IKCtl_DiagRefLvlMinErr_au16__ls_2_rs_(j) > 0.165 && IKCtl_DiagRefLvlMinErr_au16__ls_5_rs_(j) > 0.165
Epm_nEngallc2_5 = [Epm_nEngallc2_5; Epm_nEng(j)];
IKCtl_au16_2allc = [IKCtl_au16_2allc; IKCtl_DiagRefLvlMinErr_au16__ls_2_rs_(j)];
IKCtl_au16_5allc = [IKCtl_au16_5allc; IKCtl_DiagRefLvlMinErr_au16__ls_5_rs_(j)];
end
if Epm_nEng(j) > 0 && IKCtl_DiagRefLvlMinErr_au16__ls_0_rs_(j) > 0.165 && IKCtl_DiagRefLvlMinErr_au16__ls_4_rs_(j) > 0.165
Epm_nEngallc0_4 = [Epm_nEngallc0_4; Epm_nEng(j)];
IKCtl_au16_0allc = [IKCtl_au16_0allc; IKCtl_DiagRefLvlMinErr_au16__ls_0_rs_(j)];
IKCtl_au16_4allc = [IKCtl_au16_4allc; IKCtl_DiagRefLvlMinErr_au16__ls_4_rs_(j)];
end
end
end
This would be the output of plot (Epm_nEngallc0_4, IKCtl_au16_0allc, '.'); if you append them (more datapoints in the (500-2000, 0-5) region):
Hope this helps you understand the reasons behind what was happening and how to go ahead!
More Answers (1)
Walter Roberson
on 13 Sep 2024 at 4:05
You set your various arrays conditionally. We should not be surprised if the arrays come out different lengths.
You do not clear the arrays between for i iterations. The arrays are going to stay the same size as they were set in the previous iteration, with entries going unused and maintaining their previous values, unless it just so happens that the second round happens to need more entries in the arrays.
Overall, you should expect chaos in the size of the arrays.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!