help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: importdata


From: Eddy
Subject: Re: importdata
Date: Tue, 16 Nov 2010 03:04:49 -0800 (PST)


Søren Hauberg wrote:
> 
> P.S. Assuming you did not use any code from Mathworks, would you be
> interested in finishing the implementation of 'importdata' and
> publishing it for inclusion in the 'io' package?
> 
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
> 
> 

Dear all,

I'm new on this forum/mailing-list, but I'm interested in giving a
contribution, however small, for the implementation of the "importdata"
function.
I was having the same kind of problem in translating a code from Matlab to
Octave but I finally "jump" over it with the use of fgets and fscanf.
My .dat files are similar to the following example:


T1, T2, VH1, VH2, ... (and so on)
1.0 , 2.3 , 4.3 , 1-3e-3
...

so that I wrote a small routine in order to save the names of the variables
in a string array and the numerical values in a matrix.

Here you find my routine, I hope it would be useful to someone, although I
realize that the quality of programming is rather crude.

 function [posit,A] = importdata(experiment,confront)
% experiment is the name of the file to load
% confront is a string vector to be compared with the first line of the file
to load

% it saves the names of the variables in a file, going in a new line at
every comma
dat = fopen(experiment,'r');
a = fgets(dat);
A = fscanf(dat,'%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f
,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f     ,%f ,%f ,%f ,%f ,%f ,%f
,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%f
,%f ,%f ,%f ,%f',[56,     Inf]);
A = A';
b = 0;

fid = fopen("prova",'w');

for i = 1:size(a,2)
    if a(i)==","
        b = [b,i];
        c = [b+1,i+1];
    end
end

b = b(2:end);

for i = 1:length(b)
    for j = c(i):b(i)-1
        fprintf(fid,a(j))
    end
    fprintf(fid,"\n")
end
for i = c(end):size(a,2)
    fprintf(fid,a(i))
end

fclose(fid)

% it defines a string vector reading each line from the file prova created
above
fid = fopen("prova",'r');

for i = 1:length(c)-1
    s(i) = {fgets(fid)};
end

fclose(fid);

% it confronts the vector confront with the string vector s
m = length(confront);
n = length(s);

for i = 1:m
    f = length(confront{1,i});

    for j = 1:n
        g = length(s{1,j})-1;
        if f==g
            k = f;
            vect(j) = (strncmp(s(1,j),confront(1,i),k)); % for each i only
one element differs from zero

            if vect(j)==1                               % it saves the
position of the element that differs from zero at every cicle
                posit(i) = j;
            end
        end
end

It would be great if someone should help me in improving this routine (for
example the definition of A is not very portable).

Besides if somebody has better ideas for the implementation of such a
function please let me know, I would like to collaborate if possible.
-- 
View this message in context: 
http://octave.1599824.n4.nabble.com/importdata-tp2019104p3044592.html
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]