Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How to get a column from a text file based on some conditions

조회 수: 1 (최근 30일)
Gautam Kollabathula
Gautam Kollabathula 2020년 6월 6일
마감: MATLAB Answer Bot 2021년 8월 20일
Hello, the text file i have has below data
Node C: -62
Node B: -71
Node A: -72
Node C: -62
Node C: -62
Node C: -61
Node B: -69
Node A: -56
Node A: -72
Node A: -57
Node A: -56
Node C: -76
I want to read this text file and create three array's based on the node type i.e. A,B,C.
i.e. Array1 has to include the value in column3 whose column 2 is A
Array2 has to include all values from column3 whose column2 is B.
Array3 has to include all values from column3 whose column3 is C.
How do i do it? I am a novice in Matlab and am struggling to understand how to segregate this.
Thanks for your help

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 6월 6일
편집: Ameer Hamza 2020년 6월 6일
Try this
data = fileread('test.txt');
data = textscan(data, 'Node %1s: %f');
labels = [data{1}{:}]-64;
vals = data{2};
Array = accumarray(labels.', vals, [], @(x) {x});
Array is a cell array with 3 elements
Array{1} % values for node A
Array{2} % values for node B
Array{3} % values for node C

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by