May i know how can i import multiple entries such as this:
32*0 0.237024 0.248206 39*0 0.217874 0.160635
0.193782 0.242259 0.281551 37*0 0.168798 0.178275
As for the first input, i want to create 32 separate inputs of zeros. Thank you.

 채택된 답변

Mathieu NOE
Mathieu NOE 2021년 3월 16일

0 개 추천

so this is the code , maybe there is still room for improvement , but I tested it ok vs the data file provided
hope it helps
clc
clearvars
Filename = 'data.txt'
output = extract_data(Filename); % cell array
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function output = extract_data(Filename)
fid = fopen(Filename);
tline = fgetl(fid);
k = 0;
while ischar(tline)
k = k+1;
% find position where the zero product appear in each line
if contains(lower(tline),'*0') % lower make matlab not case sensitive
ind_asterix = strfind(lower(tline),'*0')
for ci = 1:length(ind_asterix)
ind_start = max(1,ind_asterix(ci)-5);
ind_stop = ind_asterix(ci)-1;
nb_of_zeros(ci) = str2num(tline(1,ind_start:ind_stop));
end
% let's split each line in individual scalars and replace the zero
% with new vector
tline_num = str2num(tline);
q = 0;
line_all = [];
for ci = 1:length(tline_num)
tline_extract = tline_num(1,ci);
if tline_extract == 0
q = q+1;
str = num2str(zeros(1,nb_of_zeros(q)));
else
str = num2str(tline_extract);
end
% concat all segements together
line_all = [line_all, ' ', str];
end
else
line_all = tline;
end
% output{k} = line_all; % array of char
output{k} = str2num(line_all); % numerical (vector)
tline = fgetl(fid);
end
fclose(fid);
end

댓글 수: 2

Jey Ganasan
Jey Ganasan 2021년 3월 17일
thank you very much, i modified it to get my desired output. It's working.
Mathieu NOE
Mathieu NOE 2021년 3월 17일
Glad it helped

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2021년 3월 16일

0 개 추천

hello
you can concatenate datas and vectors of zeros like that :
a = [zeros(1,32) 0.237024 0.248206 zeros(1,39) 0.217874 0.160635];
b = [0.193782 0.242259 0.281551 zeros(1,37) 0.168798 0.178275];

댓글 수: 3

Jey Ganasan
Jey Ganasan 2021년 3월 16일
hello there,
thank you but I'm trying to import these values into table, there are 9300 lines of random numbers, such as this
2*0 0.237024 0.248206 39*0 0.217874 0.160635
0.193782 0.242259 0.281551 37*0 0.168798 0.178275
0.156088 0.19499 0.232122 0.222636 0.295423 18*0
0.224133 0.245725 0.331081 0.387437 13*0 0.147574
0.212953 0.1945 0.171819 0.19408 0.242008 0.237876
0.292011 17*0 0.226117 0.256962 0.238144 0.451803
0.300003 12*0 0.162104 0.0541259 0.145507 0.168693
0.178204 0.207441 0.213339 0.319337 0.301409 0.304104
14*0 0.236128 0.2363 0.253809 0.303041 0.242415
0.438212 0.304733 0.248699 10*0 0.209468 0.147598
0.111917 0.131427 0.136847 0.152239 0.189117 0.231847
Mathieu NOE
Mathieu NOE 2021년 3월 16일
ok
so you have a text file , and each line must be converted to a numerical vector , with , for example " 10*0 " converted into zeros (1,10) ? am I right ?
Jey Ganasan
Jey Ganasan 2021년 3월 16일
Yes.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2021년 3월 16일

댓글:

2021년 3월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by