Hi all, I have read the contents from file and I have a cell which contians 10133x1 cell array. The cell array contains of letter and number. I have strfind function to find the position of the letter, z and I want to extract all the number below the position of z to be a 1d array. Any tips and suggestions is appreciated. I have attached the example of the data.
My code is looks like below,
inputfile = sprintf('read_data','r'); %'r' = read
fileID = fopen(inputfile,'r');
C = textscan(fileID, '%s');
loc = strfind(C{1}(:), 'z');

1 件のコメント

Stephen23
Stephen23 2021 年 6 月 10 日
編集済み: Stephen23 2021 年 6 月 10 日
@K3iTH: What does this line of code actually do?:
inputfile = sprintf('read_data','r'); %'r' = read
Why did you add a superfluous 'r' which gets ignored? What does that misleading comment refer to?

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

 採用された回答

Stephen23
Stephen23 2021 年 6 月 10 日
編集済み: Stephen23 2021 年 6 月 10 日

0 投票

"I have read the contents from file and I have a cell which contians 10133x1 cell array. The cell array contains of letter and number."
This does not seem like a very efficient way to import that data. I doubt that STRFIND is suitable for this task:
Here is a simple and very efficient approach which does not read the entire file as text:
[fid,msg] = fopen('Example.txt','rt');
assert(fid>=3,msg)
fscanf(fid,'%[^z]');
fscanf(fid,'z');
vec = fscanf(fid,'%f')
vec = 41×1
0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601
fclose(fid);
unique(vec) % in case anyone wonders, yes those values repeat in the file!
ans = 3×1
0.309016994374947 0.669130606358858 0.913545457642601
Alternatively, if you really want to import the filedata as text:
str = fileread('Example.txt');
vec = sscanf(regexprep(str,'^.+z',''),'%f')
vec = 41×1
0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601 0.913545457642601
unique(vec) % in case anyone wonders, yes those values repeat in the file!
ans = 3×1
0.309016994374947 0.669130606358858 0.913545457642601

2 件のコメント

Image Analyst
Image Analyst 2021 年 6 月 10 日
@K3iTH, let me be even more direct. Please attach your text file with the paperclip icon. Or attach your .mat file with your cell array stored inside it.
K3iTH
K3iTH 2021 年 6 月 10 日
Hi there. I have attache an example text file. Thanks

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

その他の回答 (1 件)

KSSV
KSSV 2021 年 6 月 10 日
編集済み: KSSV 2021 年 6 月 10 日

0 投票

idx = loc+1 ;
iwant = cell2mat(C{idx})

2 件のコメント

K3iTH
K3iTH 2021 年 6 月 10 日
Hi, I have tried your method but it shows the error
Operator '+' is not supported for
operands of type 'cell'.
Thanks
KSSV
KSSV 2021 年 6 月 10 日
You have the indices loc, using these indices you can try extracting cell.

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

カテゴリ

ヘルプ センター および File Exchange で Data Import and Export についてさらに検索

製品

質問済み:

2021 年 6 月 10 日

編集済み:

2021 年 6 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by