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 件のコメント

Image Analyst
Image Analyst 2018 年 12 月 31 日
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 2018 年 12 月 31 日
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 2018 年 12 月 31 日
編集済み: Louis Cook 2018 年 12 月 31 日
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

サインインしてコメントする。

回答 (1 件)

Cris LaPierre
Cris LaPierre 2018 年 12 月 31 日
編集済み: Cris LaPierre 2018 年 12 月 31 日

0 投票

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 件のコメント

Louis Cook
Louis Cook 2019 年 1 月 4 日
編集済み: Cris LaPierre 2019 年 1 月 4 日
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
Cris LaPierre
Cris LaPierre 2019 年 1 月 4 日
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];

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2018 年 12 月 30 日

コメント済み:

2019 年 1 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by