你可以用xlswrite,matlab上的类似书面列标题分组数据?

对matlab很新,仍然在学习基础知识。 我试图写一个脚本来计算波形中两个峰值之间的距离。 这部分我已经设法做了,我用xlswrite把我得到的值放到一个excel文件中。

对于每个文件,我有大约50-250列,只有两行:第二行有数值,第一行有列标题,从我从中提取数据的原始Excel文件复制。

有些列有相似但不相同的标题,例如'green227RightEyereading3'和'green227RightEyereading4'等。有没有一种方法可以将具有相似标题的列组合在一起,例如在标题中具有相同的数字/颜色(Iegreen227)和要么是“右眼”,要么是“左眼”,并计算其数值的平均值? 链接到这里文件:> https://www.dropbox.com/s/ezpyjr3raol31ts/SampleBatchForTesting.xls?dl=0 >

>[Excel_file,PathName] = uigetfile('*.xls', 'Pick a File','C:\Users\User\Documents\Optometry\Year 3\Dissertation\A-scan3'); >[~,name,ext] = fileparts(Excel_file); >sheet = 2; >FullXLSfile = [PathName, Excel_file]; >[number_data,txt_data,raw_data] = xlsread(FullXLSfile,sheet); >HowManyWide = size(txt_data); >NumberOfTitles = HowManyWide(1,2); >xlRangeA = txt_data; >Chickens = {'Test'}; >for f = 1:xlRangeA; %%defined as top line of cells on sheet; >Text = xlRangeA{f}; >HyphenLocations = find(Text == '-'); >R = HyphenLocations(1,1) -1; >Chick = Text(1:R); >Chick = cellstr(Chick); >B = length(Chick); >TF = strncmp(Chickens,Chick,B); >if any(TF == 1); %do nothing >else >Chickens = {Chickens;Chick}; >end >end 

这里也是一个链接到当我运行我的整个脚本时创build的文件。 标题下面的值是我正在分析的组织的计算厚度。 https://www.dropbox.com/s/4p6iu9kk75ecyzl/Choroid_Thickness.xls?dl=0

非常感谢

如果不同的字符位于标题的最末尾(或开头),则可以使用strncmp内置函数,并仅比较部分string。 在这里看到更多。 但是,请提供一些代码和你的Excel文件的一部分。 这将有所帮助。

另外,如果我没有弄错,你将所有的数据保存到excel中,然后再重新调用它来sorting。 也许你应该考虑在excel中保存最终结果,这会节省一些时间,特别是如果你想多次运行你的脚本。

编辑:

这是我想出的代码。 这当然不是最好的解决scheme,但它可以与你上传的文件一起工作。 我省略了不必要的行和variables。 该代码仅在每个阅读的数字具有相同数量的数字时才起作用。 只要每个条目有4位数字,它们可以是4位数字。 由于在每个文件中你都有相同颜色的波,所以你唯一关心的是阅读是用左眼还是右眼logging的(正确的?)。 根据你和你写的代码,比较涉及包含单词“右”或“左”的string部分,即连字符之间的字符。

 [Excel_file,PathName] = uigetfile('*.xls', 'Pick a File',... 'C:\Users\User\Documents\Optometry\Year 3\Dissertation\A-scan3'); sheet = 1; FullXLSfile = [PathName,Excel_file]; [number_data,txt_data,raw_data] = xlsread(FullXLSfile,sheet); %% data manipulation NumberOfTitles = length(txt_data); TextToCompare = txt_data{1}; r1 = 1; % counter for Readings1 vector r2 = 1; % counter for Readings2 vector for ff = 1:NumberOfTitles % in your code xlRangeA is a cell vector not a number! Text = txt_data{ff}; HyphenLocations = find(Text == '-'); Text = Text(HyphenLocations(1,1):HyphenLocations(1,2)); % take only the part that contains the "eye" information TextToCompare = TextToCompare(HyphenLocations(1,1):HyphenLocations(1,2)); % same here if (Text == TextToCompare) Readings1(r1) = number_data(ff); % store the numerical value in a vector r1 = r1 + 1; % increase the counter of this vector else Readings2(r2) = number_data(ff); % same here r2 = r2 + 1; end TextToCompare = txt_data{1}; % TextToCompare re-initialized for the next comparison end mean_readings1 = mean(Readings1); % Find the mean of the grouped values mean_readings2 = mean(Readings2); 

我确信这可以以更高效和更精细的方式来完成。 我不知道你想要做什么样的计算,所以我只把平均值作为例子。 在if语句中,如果需要的if也可以存储txt_data 。 下面我还列出了第二种方法,我觉得更细腻。 只要用下面的部分replace%%data manipulation部分就可以了:

 %% more delicate way Text_Vector = char(txt_data); TextToCompare2 = txt_data{1}; HyphenLocations2 = find(TextToCompare2 == '-'); TextToCompare2 = TextToCompare2(HyphenLocations2(1,1):HyphenLocations2(1,2)); Text_Vector = Text_Vector(:,HyphenLocations2(1,1):HyphenLocations2(1,2)); Text_Vector = cellstr(Text_Vector); dummy = strcmpi(Text_Vector,TextToCompare2); Readings1 = number_data(dummy); Readings2 = number_data(~dummy); 

我希望这有帮助。