Greetings,
So if i have the following:
a = '21414839.260 112535757.19909 2294.184 21414838.320 87690207.10148 1787.672'
Using:
A=sscanf(a,'%f')
Will return:
A =
1.0e+08 *
0.2141
1.1254
0.0000
0.2141
0.8769
0.0000
But if If the last 3 numbers are missing from the line :
a = '21414839.260 112535757.19909 2294.184 '
I want this to be returned:
A =
1.0e+08 *
0.2141
1.1254
0.0000
NaN
NaN
NaN
How can I make this possible?

 Respuesta aceptada

Geoff Hayes
Geoff Hayes el 6 de Jun. de 2014

1 voto

If you know that sscanf of the input string is to return 6 numbers, and A is only populated with three, then you could do the following
A = [A; repmat(NaN,6-length(A),1)];

16 comentarios

Sebastian Ciuban
Sebastian Ciuban el 6 de Jun. de 2014
Actually, I want to return NaN where is white space instead of numbers. The size of A doesn,t matter.
e.g. 6 numbers
a = '21414839.260 112535757.19909 2294.184 21414838.320 87690207.10148 1787.672'
Applies the command:
A=sscanf(a,'%f')
Returned:
A =
1.0e+08 *
0.2141
1.1254
0.0000
0.2141
0.8769
0.0000
eg. with 5 numbers
a = '21414839.260 112535757.19909 2294.184 21414838.320 87690207.10148 '
Applies the command:
A=----------
To be returned:
A =
1.0e+08 *
0.2141
1.1254
0.0000
0.2141
0.8769
NaN
And so on..If I have 4,5 numbers..where the readinf of line interceps white spaces it must return NaN
Geoff Hayes
Geoff Hayes el 6 de Jun. de 2014
But if there is just white space and no other indication, how is this function (or any other) supposed to know that there should be five numbers or six or four or none? Look at a - it is just a string with some numbers separated by whitespace which may have a block of trailing spaces when there are missing numbers. If the size of this block is x characters how do you determine how many numbers should be there? If all of your numbers had the same digit count, then you could probably make a guess…but without any other information…
How is a determined?
Sebastian Ciuban
Sebastian Ciuban el 6 de Jun. de 2014
The function is this:
function [obs] = readRINEXobs()
%Opening the selecting menu
[file,path] = uigetfile('*.**o');
%Opening the file
fid = fopen(strcat(path,file),'r');
%Reading until END of HEADER
a = fgets(fid);
while (~strcmp(a(61:63),'END'))
a = fgets(fid);
end
k = 0;
while ~feof(fid)
a = fgets(fid);
sat=a;
index=strfind(a,'G');
[~,n]=size(index);
for i=1:n
A(1,i)=str2num(sat(index(i)+1:index(i)+2));
a = fgets(fid);
A(2:6,i) = sscanf(a,'%f');
a = fgets(fid);
A(7,i) = str2num(a);
end
k = k+1;
obs(k).data=A;
end
Geoff Hayes
Geoff Hayes el 6 de Jun. de 2014
Is the problem at the line
A(2:6,i) = sscanf(a,'%f');
where you try to populate five values in A but the sscanf returns less than five (leading to the Subscripted assignment dimension mismatch.? If that is the case, then you have made an assumption on the number of floats that you are trying to read in from a and that is 5. So the above is still applicable
% read what you can from the string and format as numbers
temp = sscanf(a,'%f');
% initialize the ith column of A from rows 2 through 6 with the five
% values from temp, or those values that you could read plus NaNs
A(2:6,i) = [temp; repmat(NaN,5-length(temp),1)];
Sebastian Ciuban
Sebastian Ciuban el 6 de Jun. de 2014
Thank you! This is it :)
Geoff Hayes
Geoff Hayes el 6 de Jun. de 2014
You're welcome!
Sebastian Ciuban
Sebastian Ciuban el 6 de Jun. de 2014
Editada: Sebastian Ciuban el 6 de Jun. de 2014
Can I ask your assistance again for a small thing?
Geoff Hayes
Geoff Hayes el 6 de Jun. de 2014
For sure!
Sebastian Ciuban
Sebastian Ciuban el 7 de Jun. de 2014
This function reads blocks of data from an ASCII file. Some of them contains white space, problem which was solved with your help and Star Strider's. The thing is I get extra data in the blocks and I can't figure from where.
I think this may be too much to ask, but if I archive the function + data file..Can you hit a "Run" a take a small look?
Geoff Hayes
Geoff Hayes el 7 de Jun. de 2014
Just attach your files to a comment and I can take a look.
Sebastian Ciuban
Sebastian Ciuban el 7 de Jun. de 2014
The function will return a structure 1x2880. The number means the blocks of data that have been read..If you open in matlab the file RINEXOBS13.o, and scroll at the end of file you will see that the last block of data has 8 satellites..In my function, the last one has 12 satellites..and it does that to many blocks, adding extra information.
Sebastian Ciuban
Sebastian Ciuban el 7 de Jun. de 2014
I think the problem is in the FOR loop but I can't figure what exactly
Sebastian Ciuban
Sebastian Ciuban el 8 de Jun. de 2014
This is it
Geoff Hayes
Geoff Hayes el 9 de Jun. de 2014
Hi Ciuban - the problem is that A matrix that is being populated with the satellite information is not being reset/cleared for each block:
while ~feof(fid)
% do some stuff
[~,n]=size(index);
for i=1:n
A(1,i)=str2num(.
% etc.
So once this matrix reaches size 12 (for whatever iteration of the while loop) then it holds on to that excess data for each subsequent block that has less than 12 satellites. When the last block of eight satellites is evaluated, A keeps the last four from some previous block(s). To get around this, just re-size the A matrix prior to entering the for loop since you know how many rows (7) and you know the columns (n):
while ~feof(fid)
tline = fgets(fid);
sat=tline;
index=strfind(tline,'G');
[~,n]=size(index);
A = zeros(7,n); % add this line !!!!
for i=1:n
% etc.
Pre-allocation of a matrix is usually a good idea - it prevents a matrix from being resized on subsequent iterations (may improve speed), and clears those pesky warnings in the editor.
Also, don't forget to add in the fclose(fid); statement. Hope the above helps!
Sebastian Ciuban
Sebastian Ciuban el 9 de Jun. de 2014
I don't know how to thank you for your patience and help!
Geoff Hayes
Geoff Hayes el 9 de Jun. de 2014
It was fun!

Iniciar sesión para comentar.

Más respuestas (1)

Star Strider
Star Strider el 6 de Jun. de 2014

4 votos

Use sscanf with two output arguments:
% Known input length
L = 6;
a = '21414839.260 112535757.19909 2294.184'
[A, knt] = sscanf(a, '%f');
A(knt+1:L) = NaN;
produces:
A =
21.4148e+006
112.5358e+006
2.2942e+003
NaN
NaN
NaN

2 comentarios

Sebastian Ciuban
Sebastian Ciuban el 6 de Jun. de 2014
Thank you for your answer!
Star Strider
Star Strider el 6 de Jun. de 2014
My pleasure!

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 6 de Jun. de 2014

Comentada:

el 9 de Jun. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by