【问题标题】:How to put a specific .csv file information in a specific matrix location?如何将特定的 .csv 文件信息放在特定的矩阵位置?
【发布时间】:2014-05-13 23:55:01
【问题描述】:

我有许多文件(例如 100 个)包含 CCD 相机的 512x512 像素的强度,每个文件都处于不同的光频率。 这些文件的格式如下:

1, 1, 602
1, 2, 598
1, 3, 546

第一个数字是像素的Row,第二个是像素的Column,最后一个是像素上的Intensity。

我想为这些像素中的每一个设置一个数组。到目前为止,这是我的代码:

%the user selects the "many files"%
filenames = uigetfile('*.csv','','','Multiselect','on');  

%here to know the number of different frequency for each pixel
NumFiles = numel(filenames);  

%There are 512x512 pixels, each with NumFiles different intensities
Pixel = cell(512,512,NumFiles);  

在这部分之后,我不太确定如何进行。我希望 Pixel(1,1,:) 成为我的第一个像素的所有强度,这些信息取自每个文件。

马克-奥利维尔

【问题讨论】:

    标签: matlab file pixel text-processing


    【解决方案1】:

    试试这个 -

    %the user selects the "many files"%
    filenames = uigetfile('*.csv','','','Multiselect','on');
    
    %here to know the number of different frequency for each pixel
    NumFiles = numel(filenames);
    
    %There are 512x512 pixels, each with NumFiles different intensities
    Pixel = cell(512,512,NumFiles)
    count = 0
    num_pixels = size(Pixel,1)*size(Pixel,2)
    
    for k = 1:NumFiles
        fid = fopen(char(filenames(k)));
        C = textscan(fid, '%d, %d, %d')
        Pixel(count + sub2ind(size(Pixel),C{1},C{2})) = num2cell(C{3});
        count = count + num_pixels;
        fclose(fid);
    end
    

    【讨论】:

    • 我不太确定“计数”部分。为什么是“count = count + num_pixels;”?
    • @Marc-olivierLessard 这就是linear indexing 用于Pixel 的全部内容。第一个文件将用于索引Pixel 的第一个num_pixels 元素,第二个文件用于索引num_pixels+12*num_pixels,第三个文件用于索引2*num_pixels+13*num_pixels,依此类推,由count 分别从 0num_pixels2*num_pixels 等等。希望这是有道理的!
    • 我意识到我有一个错误,我没有指定文件路径。我添加了 [filenames,filepath] = uigetfile 现在如何正确使用 fopen?我试过“fid = fopen(char(filepath, filenames(k)));”但它不会工作。有什么想法吗?
    • 就这样做 - [filenames,filepath] = uigetfile('*.csv','','','Multiselect','on'); filenames = fullfile(filepath,filenames)。其余代码保持不变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多