help-octave
[Top][All Lists]
Advanced

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

How to return multiple values from an Oct file


From: mpender
Subject: How to return multiple values from an Oct file
Date: Sat, 22 Mar 2014 23:26:37 -0700 (PDT)

I've read through early postings that recommended using an Octave value list
to return multiple values from an Oct function of the form [a, b] =
octfunction (d, e, f);  Anyhow, I'm stuck because I think this looks
correct, but I keep getting segmentation faults when I return the Octave
list function.  I apologize that the length of code is not minimized this
time, but I am not sure what part of the code is actually causing the
problem.  So it may be unnecessarily complicated.

Any ideas?


loadtspfund.cc:

#include <octave/oct.h>
#include <stdio.h>
#include <string.h>
#define ARRAYSIZE 3000

// loadtspfund("tspQuicken.csv","TSPCFUND")

DEFUN_DLD (loadtspfund, args, nargout,  "load TSP records for a specific
named fund from a file.")
{
        octave_value_list retval;
        int nargin = args.length();

        char linebuffer[80];
        char fund[20];

        double price;
        int month;
        int day;
        int year;

        double prices[ARRAYSIZE];
        int months[ARRAYSIZE];
        int days[ARRAYSIZE];
        int years[ARRAYSIZE];

        int index;
        int jndex = 0;
        int rows;
                
        if (nargin == 2)
        {
// E.g. "tspQuicken.csv"
                std::string filename = args(0).string_value();
// E.g. "TSPCFUND"
                std::string fundname = args(1).string_value();

                FILE * fp;
//              fp = fopen ("tspQuicken.csv","r");
                fp = fopen (filename.c_str(),"r");
        
                if (fp == NULL) perror ("Error opening file");
                else 
                {
                while (fgets (linebuffer, 80, fp) != NULL)
                        {
// Preprocess the linebuffer to replace commas and slash characters with
space 
// characters so that sscanf can be used to extract the data fields.
                                for (index = 0; index < 80; index++)
                                {
                                        if (linebuffer[index]==0) break;
                                        if 
((linebuffer[index]==',')||(linebuffer[index]=='/'))
                                                linebuffer[index]=' ';
                                }
                                sscanf(linebuffer, "%s %lf %d %d %d", fund, 
&price, &month, &day,
&year);
//                              if (strcmp(fund, (const char *)fundname)==0)
                                if (strcmp(fund, fundname.c_str())==0)
                                {
//                              printf("%04d %s %6.4f %d-%d-%d\n", jndex, fund, 
price, month,
day, year);
                        prices[jndex] = price;
                                        months[jndex] = month;
                                        days[jndex] = day;
                                        years[jndex] = year;
                                        rows = jndex;
                                        jndex++;
                                }
                        }
                printf("%04d %s %6.4f %d-%d-%d\n", rows, fundname.c_str(),
prices[rows], months[rows], days[rows], years[rows]);
                        fclose (fp);
                }
// Returning an array of single precision floats.
//      return octave_value_list(jndex, fund, price, month, day, year);
        }
        Matrix Prices_tmp (rows, 1);
        Matrix Months_tmp (rows, 1);
        Matrix Days_tmp (rows, 1);
        Matrix Years_tmp (rows, 1);

    int row;
    for (row = 0; row < rows; row++)
        {
                Prices_tmp(row, 1) = prices[row];
                retval(0) = Prices_tmp;

                Months_tmp(row, 1) = months[row];
                retval(1) = Months_tmp;

                Days_tmp(row, 1) = days[row];
                retval(2) = Days_tmp;

                Years_tmp(row, 1) = years[row];
                retval(3) = Years_tmp;
        }
    return retval;
}




--
View this message in context: 
http://octave.1599824.n4.nabble.com/How-to-return-multiple-values-from-an-Oct-file-tp4663277.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

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