2017-07-28 63 views
0

我顯然無法區分不同類型的向量和數組以及像string,cellstr,char等命令。我的代碼在另一個網絡上,但基本上,我得到一個錯誤,說我的imread語句中的參數必須是字符矢量。這個參數是一個1x30的文件名數組,因爲我使用了iscell命令,並且它返回了1,所以我嘗試了上面列出的命令的幾個組合,並且一直在閱讀我能夠做的所有事情,但無法確定如何更改將1x30單元格數組轉換爲字符向量,以便imread語句可以工作。文件名是從文件夾(使用uigetfile)作爲757-01.bmp,757-02.bmp ... 757-30.bmp讀入的。我想我需要讓他們'757-01.bmp','757-02.bmp'...'757-30.bmp',並可能成爲一個30x1矢量副1x30?或者,這對代碼將在下一次遇到的for循環無關緊要。謝謝你的任何援助...在MatLab中,如何從單元格數組創建一個字符向量?

[imagefiles,file_path]=uigetfile({'*.jpeg;*.jpg;*.bmp;*.tif;*.tiff;*.png;*.gif','Image Files (JPEG, BMP, TIFF, PNG and GIF)'},'Select Images','multiselect','on'); 
imagefiles = cellstr(imagefiles); 
imagefiles = sort(imagefiles); 
nfiles = length(imagefiles); 

r = inputdlg('At what pixel row do you want to start the cropping?  .','Row'); 
r = str2num(r{l}); 

c = inputdlg('At what pixel column do you want to start the cropping (Must be > 0)?  .','Column'); 
c = str2num(c{l}); 

h = inputdlg('How many pixel rows do you want to include in the crop?  .','Height'); 
h = str2num(h{l}); 

w = inputdlg('How many pixel columns do you want to include in the crop?  .','Width'); 
w = str2num(w{l}); 

factor = inputdlg('By what real number factor do you want to enlarge the cropped image?  .','Factor'); 
factor = str2num(factor{l}); 

outdimR = h * factor; 
outdimC = w * factor; 

for loop=l:nfiles 
    filename = imagefiles(loop); 
    [mybmp, map] = imread(filename); 
    myimage = ind2rgb(mybmp,map); 
    crop = myimage(r:r+h-1, c:c+w-1, :); 
    imwrite(crop, sprintf('crop757-%03i.bmp',loop)); 
end 
+1

你可以發表你的代碼如何使用imread?這可能是你的錯誤所在。 – Flynn

+2

請發表[mcve]。 – beaker

+0

代碼是裁剪圖像: – bpfreefly

回答

0

關於你原來的問題:

在MATLAB中,我怎麼可以創建一個單元陣列的字符向量?

可以使用電池訪問操作數({}),以獲得您的單元格中字符串作爲@本 - 沃伊特指出,或者包裹char()在你的聲明(我和Ben的建議去)。

關於你提到的後續問題:說文件「757-01.bmp」根本不存在imread

它的錯誤了。在imagefiles數組中,30個值中的第一個是757-01.bmp(不包括引號),但我不知道MatLab是否將引號放在文件名的周圍意味着它在數組中查找帶引號的值。

這聽起來像你的文件是在另一個目錄,而不是你正在運行你的代碼。

使用fullfile創建完整的文件名以使用文件的絕對路徑而不是相對路徑(這不適用於此)。

假設您的文件位於路徑~/bpfreefly/images/中。

然後你就可以改變你的代碼是這樣的:

imgPath = '~/bpfreefly/images/' 
for loop=l:nfiles 
    filename = fullfile(imgPath,imagefiles{loop}); 
    [mybmp, map] = imread(filename); 
    myimage = ind2rgb(mybmp,map); 
    crop = myimage(r:r+h-1, c:c+w-1, :); 
    imwrite(crop, sprintf('crop757-%03i.bmp',loop)); 
end 

順便說一句,你可以從uigetfile的第二輸出參數的路徑名。

+0

謝謝大家! – bpfreefly

相關問題