How do you perform moving average with given data?

3 ビュー (過去 30 日間)
Sarah Mullin
Sarah Mullin 2021 年 2 月 22 日
回答済み: Image Analyst 2021 年 2 月 22 日
I was given a csv file to download and I used this code below to read it.
clear;
fclose all;
fileID = fopen('accidents_2017.csv');
text1 = textscan(fileID,'%s%s%s%s%s%s%d%d%s%d%d%d%d%f%f','HeaderLines',1,'Delimiter',{','},'EmptyValue',NaN);
fclose(fileID);
I also used str2double(text1) to change all the strings to numbers.
I was asked to perform the moving average of the data and then plot it. How do i do so? Is there a function to do so with lots of data points?
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 22 日
Your text1 would be a cell array of arrays, not a cell array of character vectors. str2double only works on character vectors or cell array of character vectors, or string objects. You would need to extract parts of text1 such as str2double(text1{1})
But what is the purpose of reading with a %s format and then str2double when you could just use a %f format directly?
Sarah Mullin
Sarah Mullin 2021 年 2 月 22 日
Thats how my teacher told us to do it when we used textscan. He then asked us to do the following in order:
Download the dataset, Use "textscan" to read in data, Convert data into numbers, Count how many "NaN"s, Remove the "NaN"s, plot the data, De-trend and re-plot the data, and Perform moving average and replot the data.

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

採用された回答

Image Analyst
Image Analyst 2021 年 2 月 22 日
Try readmatrix()
data = readmatrix(filename);
data(isnan(data)) = [];

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by