2016-07-04 55 views
0

我有幾個數據集,名爲'51.raw''52 .raw'... until '69 .raw',並且在我的代碼中運行這些數據集後,這些數據集的大小從375x91x223變爲具有變化的y維度的尺寸(即,'51 .raw'輸出:375x45x223; '52 .raw'輸出:375x50x223,...與每個數據集不同)。將數據集的大小信息添加到文件名

我想稍後使用此信息保存'.raw'文件名(即'51_375x45x223.raw'),並且還希望使用新的數據集大小稍後在我的代碼中重新設計數據集。我試圖這樣做,但需要幫助:

for k=51:69 

data=reshape(data,[375 91 223]); % from earlier in the code after importing data 

% then executes code with dimensions of 'data' chaging to 375x45x223, ... 

length=size(data); dimensions.([num2str(k)]) = length; %save size in 'dimensions'. 

path=['C:\Example\']; 
name= sprintf('%d.raw',k); 

write([path name], data); 
% 'write' is a function to save the dat in specified path and name (value of k). I don't know how to add the size of the dataset to the name. 

而且後來我想重塑數據集的「數據」本次迭代,做一個重塑新Y尺寸值。

i.e. data=reshape(data,[375 new y-dimension 223]); 

您的幫助將不勝感激。謝謝。

+0

爲什麼不救尺寸*內*您在標題行的文件?這要好得多,試圖使用文件名傳達有關它的內容的信息 – Suever

+0

感謝您的評論Suever,輸出保存爲.raw文件。然後爲了在其他軟件(ImageJ)中打開它,如果其他人想要這樣做會更方便。另外你提到的也是我的想法,但我被要求將其更改爲文件名,但我不知道如何。 –

回答

1

你可以很容易地將你的尺寸轉換爲一個字符串,將被保存爲一個文件。

% Create a string of the form: dim1xdim2xdim3x... 
dims = num2cell(size(data)); 
dimstr = sprintf('%dx', dims{:}); 
dimstr = dimstr(1:end-1); 

% Append this to your "normal" filename 
folder = 'C:\Example\'; 
filename = fullfile(folder, sprintf('%d_%s.raw', k, dimstr)); 

write(filename, data); 

話雖這麼說,那最好是包括內這個維度信息文件本身,而不是依賴於文件名。

作爲便箋,請避免將內部函數的名稱用作變量名稱,如lengthpath。這可能會導致未來出現奇怪和意外的行爲。

更新

如果您需要解析的文件名,你可以使用textscan做到這一點:

filename = '1_2x3x4.raw'; 

ndims = sum(filename == 'x') + 1; 
fspec = repmat('%dx', [1 ndims]); 
parts = textscan(filename, ['%d_', fspec(1:end-1)]); 

% Then load your data 

% Now reshape it based on the filename 
data = reshape(data, parts{2:end}); 
+0

謝謝Suever的幫助。您如何回憶新的y維度(例如49)以從字符串「重塑」?因此,對於第二個循環,我希望它讀取尺寸'dim1xdim2xdim3',然後重塑:data = reshape(data,[dim1 dim2 dim3]) –

+0

@ a.kk添加了一些更新後的代碼以讀取尺寸文件名 – Suever

+0

謝謝Suever!我試過運行這個,但是我得到了一個錯誤,我運行了我的數據和其他數據:'使用重整的錯誤 重新設置元素的數量不能改變。' –

相關問題