Loop through csv file -- please help

6 ビュー (過去 30 日間)
Jenna Ceraso
Jenna Ceraso 2022 年 4 月 3 日
コメント済み: Stephen23 2022 年 4 月 4 日
%Hi! I am trying to step through a csv file to covert each video time into a frames. Ideally, I want to rewrite a new column in the csv column to be the converted frames. The video times are not equally incremented. The data is in a %csv file. frame = ([time in csv file]*Tframe)/Vtime where Tframe is total frames & Vtime is the total video time. Thanks!
%Video time to frames
%user entered data
Vtime = input('Length of video: '); %total length of video
Tframe = input('Total # of frames: '); %total number of frames
fid=fopen('test.csv', 'r')
k=length(fid);
while ~feof(fid)
k=k+1;
Z{k}=fgetl(fid);
frame = (Z{k}*Tframe)/Vtime;
end
fclose(fid)
x=[frame]
%The wrong values and the wrong number of values are being returned. Thanks for your help!
  1 件のコメント
Image Analyst
Image Analyst 2022 年 4 月 4 日
What is in the CSV file? Individual frame times, or image data? Or both?

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

採用された回答

Voss
Voss 2022 年 4 月 4 日
Note that fgetl returns a character vector, so multiplying that by Tframe isn't what you mean to do. You can try converting that character vector to a numeric value using str2double like this:
%Video time to frames
%user entered data
Vtime = input('Length of video: '); %total length of video
Tframe = input('Total # of frames: '); %total number of frames
fid=fopen('test.csv', 'r');
k = 0;
while ~feof(fid)
k=k+1;
Z(k)=str2double(fgetl(fid));
frame(k) = (Z(k)*Tframe)/Vtime;
end
fclose(fid);
x=[frame]
This assumes that each line has one number signifying a time on it and nothing else.
In any case, it's probably better and easier to read the file using readmatrix or readcell (or xlsread or csvread if you have an older version of MATLAB), depending on what all is in the .csv file and what all you need out of it.
  4 件のコメント
Voss
Voss 2022 年 4 月 4 日
@Jenna Ceraso You're welcome!
Stephen23
Stephen23 2022 年 4 月 4 日
"it's probably better and easier.." and faster and a better use of your time "...to read the file using readmatrix"

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 4 月 4 日
fid is the file handles so there is no reason to set k equal to it and have it be some kind of line counter.
Use readmatrix() or csvread() to read in the data from the CSV file, not fgetl(). You'll get the whole matrix at once and don't need to get it a line at a time. Your Z{k} would be a text string anyway, so that doesn't make sense.
  1 件のコメント
Jenna Ceraso
Jenna Ceraso 2022 年 4 月 4 日
Good to know. Thanks!

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

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by