分配的Octave变量未保存到文件中

时间:2017-09-22 22:14:47

标签: octave

在下面的Octave脚本中,我循环遍历目录中的文件,将它们加载到Octave中以对数据进行一些操作,然后尝试将操纵数据(矩阵)写入名称源自的新文件输入文件的名称。操纵的数据被分配给与要保存的文件同名的变量名。清除所有不需要的变量,保存命令 保存/写入单个分配的变量矩阵到文件“new_filename”。

但是,最后一次保存/写入命令没有发生,我不明白为什么不这样做。如果没有特定的变量命令,save函数应该保存范围内的所有变量,在这种情况下,只有一个要保存的矩阵。为什么这不起作用?

clear all ;

all_raw_OHLC_files = glob( "*_raw_OHLC_daily" ) ; % cell with filenames matching *_raw_OHLC_daily

for ii = 1 : length( all_raw_OHLC_files ) % loop for length of above cell

filename = all_raw_OHLC_files{ii} ; % get files' names

% create a new filename for the output file
split_filename = strsplit( filename , "_" ) ;
new_filename = tolower( [ split_filename{1} "_" split_filename{2} "_ohlc_daily" ] ) ;

% open and read file
fid = fopen( filename , 'rt' ) ;
data = textscan( fid , '%s %f %f %f %f %f %s' , 'Delimiter' , ',' , 'CollectOutput', 1 ) ;
fclose( fid ) ;
ex_data = [ datenum( data{1} , 'yyyy-mm-dd HH:MM:SS' ) data{2} ] ; % extract the file's data

% process the raw data in to OHLC bars
weekday_ix = weekday( ex_data( : , 1 ) ) ;
% find Mondays immediately preceeded by Sundays in the data
monday_ix = find( ( weekday_ix == 2 ) .* ( shift( weekday_ix , 1 ) == 1 ) ) ;
sunday_ix = monday_ix .- 1 ;

% replace Monday open with the Sunday open
ex_data( monday_ix , 2 ) = ex_data( sunday_ix , 2 ) ;

% replace Monday high with max of Sunday high and Monday high
ex_data( monday_ix , 3 ) = max( ex_data( sunday_ix , 3 ) , ex_data( monday_ix , 3 ) ) ;

% repeat for min of lows
ex_data( monday_ix , 4 ) = min( ex_data( sunday_ix , 4 ) , ex_data( monday_ix , 4 ) ) ;

% combines volume figures
ex_data( monday_ix , 6 ) = ex_data( sunday_ix , 6 ) .+ ex_data( monday_ix , 6 ) ;

% now delete the sunday data
ex_data( sunday_ix , : ) = [] ;

assignin( "base" , tolower( [ split_filename{1} "_" split_filename{2} "_ohlc_daily" ] ) , ex_data )

clear ans weekday_ix sunday_ix monday_ix ii filename split_filename fid ex_data data all_raw_OHLC_files

% print to file
save new_filename

endfor

1 个答案:

答案 0 :(得分:1)

save new_filename将当前工作空间保存到文件名为" new_filename"的文件中。我想你想要的是创建一个文件名存储在" new_filename"中的文件:

save (new_filename);

您当前的方法是清除所有我不需要的东西然后存储整个工作区"恕我直言非常丑陋,如果这是你想要的唯一部分,你应该明确地存储ex_data

save (new_filename, "ex_data");