Help creating a for loop for research purposes

1 visualizzazione (ultimi 30 giorni)
Attached is the data I am using. Each frame is seperated by 1340 cells. I have some code to do what I want for one frame but I am struggling on how to expand this to a for loop to do the same thing for all 60 frames.
%Workspace must be cleared before using this script
clear
clc
%Input the folder location containing data that is being analyzed
MainFolder = 'C:\Users\nefwa\Documents\AcetoneResearch\07_16_2020_TempCheck';
cd(MainFolder)
rawdatafile = dir('*.csv');
rawdata = rawdatafile.name;
RawDataMatrix = csvread(rawdata, 1,0);
%Creates Rawdata folder in MainFolder
mkdir('Rawdata')
cd('Rawdata')
%Converting .csv file to .dat file
[~,fnm] = fileparts(rawdata);
rawdat = sprintf('%s.dat',fnm);
dlmwrite(rawdat, RawDataMatrix)
raw = '\Rawdata';
RawDir= strcat(MainFolder, raw);
%% Data
%Changes to first folder and then to Finaldata folder
cd(MainFolder)
cd('Rawdata')
%Searches for Acetone data
Tempfile = dir('*.dat');
Tempdata1 = Tempfile.name;
Tempdata = importdata(Tempdata1);
% N2 Main Peak Sum
N2peak = sum(Tempdata(436:461,3));
%N2 low rotational lines
N2low = sum(Tempdata(476:491,3));
%N2 high rotational lines
N2high = sum(Tempdata(520:610,3));
%H20 Main Peak Sum
H20peak = sum(Tempdata(1279:1303,3));
So in this code I am finding the sum of these peaks and rotational lines for one frame currently. I am hoping to expand this into a for loop to encompass all 60 frames I have (seperated by 1340 cells). I also would like to have each sum saved as like N2peak{1}, N2peak{2}, etc. Thank you in advanced for the help!

Risposta accettata

Sudheer Bhimireddy
Sudheer Bhimireddy il 4 Ago 2020
You have frame numbers in the first column of the file, which can be used to build a cell array.
Try this:
RawDataMatrix = csvread('2020_July_16_12_34_43.csv', 1,0);
% After reading your matrix, give the frame ID where you would like to start
frame_id = 2;
frame_count = 1;
i_start = 1;
% Initialize cell array for frames
nFrames = 60;
Temp_frame_array = cell(nFrames,1);
% Start loop
for i = 1:size(RawDataMatrix,1)
if RawDataMatrix(i,1) == frame_id
% Do Nothing
else
% Store eveything till this row in to a cell
Temp_frame_array{frame_count,1} = RawDataMatrix(i_start:i-1,:);
% Update the row number for next set
i_start = i;
% update frame_id, frame_count and resume binning
frame_id = RawDataMatrix(i,1);
frame_count = frame_count + 1;
end
end
% For the last frame the IF condition won't go to ELSE, so
Temp_frame_array{frame_count,1} = RawDataMatrix(i_start:end,:);
% Now loop through frames
for i = 1:nFrames
Tempdata = Temp_frame_array{i,1};
N2peak(i,1) = sum(Tempdata(436:461,3));
N2low(i,1) = sum(Tempdata(476:491,3));
N2high(i,1) = sum(Tempdata(520:610,3));
H20peak(i,1) = sum(Tempdata(1279:1303,3));
end
Hope this helps.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by