help-octave
[Top][All Lists]
Advanced

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

nice peace of hack: make read csv-data-files more comfortable


From: Stefan Pofahl
Subject: nice peace of hack: make read csv-data-files more comfortable
Date: Thu, 14 Dec 2006 17:55:56 +0100
User-agent: Thunderbird 1.5.0.8 (Windows/20061025)

Hello!

Here is a nice peace of hack:
In the example is also included a subroutine:
"mylib_file_selection"
and together with this subroutine you can
open a csv-file with a menu-selection.

Have fun

address@hidden

P.S.:
(short remark: "dlmread" seems to be buggy,
third element of RANGE (Row of lower right corner)
is not working under 2.1.73, so please check,
if it is a version problem, I work under win32
and I don't know how to compile the actual
version)

#########################################################

function readcsv()
  # -----------------------------------------------------------------------
  # -- Loadable Function: DATA = dlmread (FILE)
  # -- Loadable Function: DATA = dlmread (FILE,SEP)
  # -- Loadable Function: DATA = dlmread (FILE,SEP,RU,CU)
  # -- Loadable Function: DATA = dlmread (FILE,SEP,RANGE)
  #     Read the matrix DATA from a text file The RANGE parameter must be
  #     a 4 element vector containing  the upper left and lower right
  #     corner [RUL,CUL,RLR,CLR]
  # Seperator:
  SEP= ";";
  # Row upper left corner:
  RUL= 1;
  # Column upper left corner:
  CUL= 1;
# Row lower right corner (this argument is not working in Vers. 2.1.73) :
  RLR= 2;
  # Column lower right corner:
  CLR= 2;
  # --- End of manual Input Data ------------------------------------------
  # -----------------------------------------------------------------------
  # make selection, you need the function "mylib_file_selection"
  # it is includet at the end of this file:
  file_selection=mylib_file_selection("*.csv");
  # The lowest index value is zero. So we have to lower indices
  Range= [RUL-1, CUL-1, RLR-1, CLR-1];
  keyboard;
  str_argument = ["(""", file_selection, """, """, SEP, """ , "];
  # equivalent trick with "eval" found in "loadimage.m"
eval (sprintf ("%s dlmread %s Range %s", "DATA= ", str_argument, ");"));
  keyboard;
# -------- function code "mylib_file_selection" -------------------------
#  you need this function, put it in your search path:
#  ------------------------------------------------------------------------
#  function selected_file=mylib_file_selection(pattern)
#    # author: address@hidden
#    fflush(stdout); # flush buffer to the standard output
#    dummy= saving_history;
#    saving_history=false;# stop writing to history file
#    # --------------------------------------------------------------------
#    if ((nargin==0)|| not(ischar(pattern)))
#      pattern="*";
#      printf("\nDefault file name pattern ""*"" will be taken.\n\n");
#    endif
#    fn_cell_array=glob(pattern);
#    if (size(fn_cell_array)(1)>25)
#      saving_history=dummy;
# error(["\n\nSorry: To manny files with the choosen name-pattern ", pattern, "!\n\n"]);
#    endif
#    while (1)
#      last_menu_entry_m1= size(fn_cell_array)(1);# m1= menu_1
#      menu_item_number_m1 = -1;
# while (menu_item_number_m1 > last_menu_entry_m1 || menu_item_number_m1 < 1)
#        printf("\nChoose the file you want:\n");
#        menu_prompt= ["Overview of files with the pattern: ", pattern];
#        fflush(stdout);
#        menu_item_number_m1= menu(menu_prompt, fn_cell_array{:});
#      endwhile
#      break;
#    endwhile # while (1)
#    selected_file= fn_cell_array{menu_item_number_m1};
#    saving_history=dummy;# set back variable saving_history
#  endfunction
endfunction



function readcsv()
  # -----------------------------------------------------------------------
  # -- Loadable Function: DATA = dlmread (FILE)
  # -- Loadable Function: DATA = dlmread (FILE,SEP)
  # -- Loadable Function: DATA = dlmread (FILE,SEP,RU,CU)
  # -- Loadable Function: DATA = dlmread (FILE,SEP,RANGE)
  #     Read the matrix DATA from a text file The RANGE parameter must be
  #     a 4 element vector containing  the upper left and lower right
  #     corner [RUL,CUL,RLR,CLR] 
  # Seperator:
  SEP= ";";
  # Row upper left corner:
  RUL= 1;
  # Column upper left corner:
  CUL= 1;
  # Row lower right corner (this argument is not working in Vers. 2.1.73) :  
  RLR= 2;
  # Column lower right corner:
  CLR= 2;
  # --- End of manual Input Data ------------------------------------------
  # -----------------------------------------------------------------------
  # make selection, you need the function "mylib_file_selection"
  # it is includet at the end of this file:
  file_selection=mylib_file_selection("*.csv");  
  # The lowest index value is zero. So we have to lower indices
  Range= [RUL-1, CUL-1, RLR-1, CLR-1];
  keyboard;
  str_argument = ["(""", file_selection, """, """, SEP, """ , "];
  # equivalent trick with "eval" found in "loadimage.m"
  eval (sprintf ("%s dlmread %s Range %s", "DATA= ", str_argument, ");"));  
  keyboard;
#  --------  function code "mylib_file_selection" -------------------------  
#  you need this function, put it in your search path:
#  ------------------------------------------------------------------------
#  function selected_file=mylib_file_selection(pattern)
#    # author: address@hidden
#    fflush(stdout); # flush buffer to the standard output
#    dummy= saving_history;
#    saving_history=false;# stop writing to history file
#    # --------------------------------------------------------------------
#    if ((nargin==0)|| not(ischar(pattern)))
#      pattern="*";
#      printf("\nDefault file name pattern ""*"" will be taken.\n\n");
#    endif
#    fn_cell_array=glob(pattern);
#    if (size(fn_cell_array)(1)>25)
#      saving_history=dummy;
#      error(["\n\nSorry: To manny files with the choosen name-pattern ", 
pattern, "!\n\n"]);
#    endif
#    while (1)
#      last_menu_entry_m1= size(fn_cell_array)(1);# m1= menu_1 
#      menu_item_number_m1 = -1;
#      while (menu_item_number_m1 > last_menu_entry_m1 || menu_item_number_m1 < 
1)
#        printf("\nChoose the file you want:\n");
#        menu_prompt= ["Overview of files with the pattern: ", pattern];
#        fflush(stdout);
#        menu_item_number_m1= menu(menu_prompt, fn_cell_array{:});
#      endwhile
#      break;
#    endwhile # while (1)  
#    selected_file= fn_cell_array{menu_item_number_m1};
#    saving_history=dummy;# set back variable saving_history
#  endfunction
endfunction

reply via email to

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