'%R1P,0,0:0,0.452743429299473,0.000005129603507,0.0000002,0,0.000002,0,0,25899036'
How could I extract the 4th,5th,10th and 11th data from this string to a matrix?

 採用された回答

Voss
Voss 2022 年 4 月 30 日

1 投票

Maybe something like this:
str = '%R1P,0,0:0,0.452743429299473,0.000005129603507,0.0000002,0,0.000002,0,0,25899036';
% split str on commas into a cell array:
c = strsplit(str,',')
c = 1×11 cell array
{'%R1P'} {'0'} {'0:0'} {'0.452743429299473'} {'0.000005129603507'} {'0.0000002'} {'0'} {'0.000002'} {'0'} {'0'} {'25899036'}
% convert elements [4 5 10 11] to numeric:
m = str2double(c([4 5 10 11]));
% display elements of m separately:
format long
for ii = 1:numel(m)
disp(m(ii));
end
0.452743429299473 5.129603507000000e-06 0 25899036

9 件のコメント

N/A
N/A 2022 年 4 月 30 日
Thanks ,
is it possible to make a matrix of these extracted values ?
Voss
Voss 2022 年 4 月 30 日
m is a matrix, a 1-by-4 matrix
str = '%R1P,0,0:0,0.452743429299473,0.000005129603507,0.0000002,0,0.000002,0,0,25899036';
c = strsplit(str,',');
m = str2double(c([4 5 10 11]))
m = 1×4
1.0e+07 * 0.0000 0.0000 0 2.5899
N/A
N/A 2022 年 4 月 30 日
Thanks, then how could I add headings for each data and a number count for data
Voss
Voss 2022 年 4 月 30 日
headings = repmat({''},1,numel(m));
number_count = zeros(1,numel(m));
N/A
N/A 2022 年 4 月 30 日
i need a matrix with headings No A B C D where No is the count and the 4 data under respective headings
Voss
Voss 2022 年 4 月 30 日
Something like this?
str = '%R1P,0,0:0,0.452743429299473,0.000005129603507,0.0000002,0,0.000002,0,0,25899036';
c = strsplit(str,',');
m = str2double(c([4 5 10 11]));
headings = ['No' sprintfc('heading_%d',1:numel(m))];
C = [headings; 1 num2cell(m)]
C = 2×5 cell array
{'No'} {'heading_1'} {'heading_2' } {'heading_3'} {'heading_4'} {[ 1]} {[ 0.4527]} {[5.1296e-06]} {[ 0]} {[ 25899036]}
N/A
N/A 2022 年 4 月 30 日
yes
N/A
N/A 2022 年 4 月 30 日
hw to chnge the headings
Voss
Voss 2022 年 4 月 30 日
One way is to type them out:
headings = {'No' 'whatever' 'they' 'should' 'be'};

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

その他の回答 (1 件)

dpb
dpb 2022 年 4 月 30 日
編集済み: dpb 2022 年 4 月 30 日

1 投票

The dead-ahead approach...
>> tok=split(s,',');
>> str2double(tok([4:5 10:11]))
ans =
0.45274
5.1296e-06
0
2.5899e+07
>>
A different solution to the second Q? would be more like
VARS=[4 5 10 11]; % the wanted variables
tok=strsplit(s,',');
vNames=compose('Var%02d',1:numel(VARS));
tData=array2table(str2double(tok(VARS)),'VariableNames',vNames);
The results of the last plus how to change names example are:
>> tData=array2table(str2double(tok(VARS)),'VariableNames',vNames)
tData =
1×4 table
Var01 Var02 Var03 Var04
_______ __________ _____ __________
0.45274 5.1296e-06 0 2.5899e+07
>> tData.Properties.VariableNames(2)={'MyNewName'}
tData =
1×4 table
Var01 MyNewName Var03 Var04
_______ __________ _____ __________
0.45274 5.1296e-06 0 2.5899e+07
>>
This takes advantage that the table has the facility for variable names built into the class as an integral part of the class instead of confusing data and the title into a single variable from which it always has to be extracted in order to use the data in the array....and, it allows the data to be of any type whereas adding a variable name for a numeric array requires that the whole array then be a cell array to hold the disparate data types.

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

N/A
2022 年 4 月 30 日

編集済み:

dpb
2022 年 4 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by