分配的 Octave 变量未保存到文件
Assigned Octave variable not being saved to file
在下面的 Octave 脚本中,我循环访问目录中的文件,将它们加载到 Octave 中以对数据进行一些操作,然后尝试将操作数据(矩阵)写入名称为的新文件源自输入文件的名称。被操纵的数据被分配给一个变量名,该变量名与要保存的文件同名。所有不需要的变量都被清除,保存命令 should save/write单个分配给文件的变量矩阵 "new_filename."
但是,最后一个 save/write 命令没有执行,我不明白为什么没有执行。如果没有特定的变量命令,保存函数应该保存范围内的所有变量,在这种情况下只有一个矩阵可以保存。为什么这不起作用?
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
save new_filename
将当前工作区保存到文件名为 "new_filename" 的文件中。我想你想要的是创建一个文件,文件名存储在 "new_filename":
save (new_filename);
您当前 "clearing all I don't need and then store the whole workspace" 的方法恕我直言,非常丑陋,如果这是您唯一想要的部分,您应该显式存储 ex_data
:
save (new_filename, "ex_data");
在下面的 Octave 脚本中,我循环访问目录中的文件,将它们加载到 Octave 中以对数据进行一些操作,然后尝试将操作数据(矩阵)写入名称为的新文件源自输入文件的名称。被操纵的数据被分配给一个变量名,该变量名与要保存的文件同名。所有不需要的变量都被清除,保存命令 should save/write单个分配给文件的变量矩阵 "new_filename."
但是,最后一个 save/write 命令没有执行,我不明白为什么没有执行。如果没有特定的变量命令,保存函数应该保存范围内的所有变量,在这种情况下只有一个矩阵可以保存。为什么这不起作用?
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
save new_filename
将当前工作区保存到文件名为 "new_filename" 的文件中。我想你想要的是创建一个文件,文件名存储在 "new_filename":
save (new_filename);
您当前 "clearing all I don't need and then store the whole workspace" 的方法恕我直言,非常丑陋,如果这是您唯一想要的部分,您应该显式存储 ex_data
:
save (new_filename, "ex_data");