How to create a loop for this excel data

So i currently have this code
clear all,close all
%InputData%
inputdata=xlsread('01.csv');%extract all data from first jump%
idx = find(inputdata(:,5) == 0);
zeroMatrix = inputdata(idx,5);
oneMatrix=zeroMatrix+1
t=sum(oneMatrix)*0.001
%Variables%
g=-9.81
%Calculations%
MH1=0.5*-g*(t^2)
This code basically takes data from a excel spreasheet (number 1) and works out MH1, I want to create a loop so that matlab takes spreadsheet 2 and creates MH2, with spread sheet 3 creating MH3 and so on and so on, I have over 50 spreadsheets to work with.
Thankyou :)

3 Kommentare

Image Analyst
Image Analyst am 31 Dez. 2018
I don't see any Excel workbook.
Also you forgot to attach the CSV text file '01.csv'. Please attach this text file so we can run your code.
And also attach the Excel workbook(s) that "spreadsheet 2" and "spreadsheet 3" are in.
Image Analyst
Image Analyst am 31 Dez. 2018
Are you simply confused about how to use the code in the FAQ to transfer text from CSV text files to Excel workbooks using the csvread() and xlswrite() functions in the middle of the loop over all text files?
Louis Cook
Louis Cook am 31 Dez. 2018
Bearbeitet: Louis Cook am 31 Dez. 2018
So the code works with the text files and thats not the issue.
The issue is that I would like to loop over all the text files so that matlab runs through all CSV text files and creates n outputs

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cris LaPierre
Cris LaPierre am 31 Dez. 2018
Bearbeitet: Cris LaPierre am 31 Dez. 2018

0 Stimmen

You need to capture all the fieldnames at once. Then have the rest of your code execute inside a for loop, once for each file.
fileNames = {'Name1.csv','Name2.csv'}
for f = 1:length(fileNames)
inputdata=xlsread(fileNames{f});%extract all data from first jump%
idx = find(inputdata(:,5) == 0);
zeroMatrix = inputdata(idx,5);
oneMatrix=zeroMatrix+1
t=sum(oneMatrix)*0.001
%Variables%
g=-9.81
%Calculations%
MH(f).data=0.5*-g*(t^2)
end
Note I changed how the data gets stored from a matrix to a structure. Now you'd access the data using MH(1).data, MH(2).data, etc.

2 Kommentare

Louis Cook
Louis Cook am 4 Jan. 2019
Bearbeitet: Cris LaPierre am 4 Jan. 2019
Hello,
So this code works perfect the only problem is that I have 105 csv files to read in,
is there anyway I can change the JumpData={'01.csv','02.csv','03.csv','04.csv','05.csv','06.csv',} part of the code in a way in which it selects all data from 01.csv to 105.csv
thankyou :)
clear all,close all
%InputData%
JumpData={'01.csv','02.csv','03.csv','04.csv','05.csv','06.csv',}
for f=1:length(JumpData );
inputdata=xlsread(JumpData{f});%extract all data from first jump%
idx = find(inputdata(:,5) == 0);
zeroMatrix = inputdata(idx,5);
oneMatrix=zeroMatrix+1;
t=sum(oneMatrix)*0.001;
%Variables%
g=-9.81;
%Calculations%
MH(f).data=0.5*-g*(t^2);
end
MH().data
You could use uigetfile with multiselect on.
If you prefer to have them hardcoded, I'd do something like this (in 18b)
num = 10:105;
ext = ".csv";
JumpData = "0" + [1:9] + ext;
JumpData = [JumpData, num+ext];

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 30 Dez. 2018

Kommentiert:

am 4 Jan. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by