How to convert zero to 'Nan' for multiple dot mat file containing 1d data

1 回表示 (過去 30 日間)
Yared Daniel
Yared Daniel 2021 年 5 月 25 日
コメント済み: Jan 2021 年 5 月 25 日
I have several dot mat files, each files contains 1d array data, some of the data has zero value, and I want to convert zero values to ‘Nan’ for each dot mat files using for loop. I did for a single file using the following commands and it successfully converted zeros to Nan.
>> clear
>> load('NormFHR1.mat')
>> A=x;
>> A(A ==0) = NaN;
>> save('MRN1','A')
However performing for each file one by one like this is a kind of tiresome since I have thousands of files. Please forward any trick to solve this problem.
Thanks for your Help

採用された回答

Matt J
Matt J 2021 年 5 月 25 日
編集済み: Matt J 2021 年 5 月 25 日
fnames=ls('NormFHR*.mat');
for i=1:size(fnames,1)
fn=fnames(i,:);
s=load(fn);
A=s.x;
A(~A)=nan;
save( strrep(fn,'NormFHR','MRN'), 'A')
end
  4 件のコメント
Yared Daniel
Yared Daniel 2021 年 5 月 25 日
Thanks you so much Mr Matt
Jan
Jan 2021 年 5 月 25 日
@Matt J: Look into the source code of ls() . It calls dir() also and convertes the field "name" to a char. The output is padded, such that all lines have the same number of columns. Onbatining the lines later forward the padded char vectors as input to load. Therefore ls is löess efficient than using the unpadded char vectors directly:
fileList = dir('NormFHR*.mat');
for k = 1:numel(fileList)
fn = fileList(k).name;
...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by