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
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 Exchange에서 Loops 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