Matlab输入格式 [英] Matlab input format

查看:295
本文介绍了Matlab输入格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  65910 / A 
22 9 4 2
9 10 4 1
2 5 2 0
4 1 1 0
65910 / T
14 7 0 4
8 4 0 2
1 2 0 0
1 1 1 1



我需要输入第一行是% d 和%c ,其中 / code> 4x4 整数矩阵。我需要在矩阵上做一些工作,然后用标题信息来标识它们。

如何在MATLAB中使用这种输入格式? 解决方案

由于您的文件包含的数据可能被视为结构化(或格式化,如果使用MATLAB的术语),您可以使用 textscan 函数来读取其内容。这个函数的主要优点是你不需要指定你的header + data结构出现的次数 - 函数只是一直持续下去,直到到达文件结尾。

给定一个具有以下结构的输入文件(我们称之为 q35853578.txt ):

  65910 / A 
22 9 4 2
9 10 4 1
2 5 2 0
4 1 1 0
65910 / T
14 7 0 4
8 4 0 2
1 2 0 0
1 1 1 1


   > 函数[data,headers] = q35853578(文件路径)
%//默认输入
if nargin< 1
filepath ='q35853578.txt';
end
%//定义常量
N_ROWS = 4;
VALS_PER_ROW = 4;
NEWLINE ='\r\\\
';
%//读取结构化文件内容
fid = fopen(filepath);
headers = textscan(fid,['%u /%c'repmat([NEWLINE repmat('%u',1,VALS_PER_ROW)],1,N_ROWS)]);
fclose(fid);
%//解析内容并准备输出
data = cell2mat(reshape(cellfun(@(x)reshape(x,1,1,[]),headers(3:end)...
'UniformOutput',false),VALS_PER_ROW,N_ROWS)。'); %'
headers = headers(1:2);
%//输出检查
如果nargout< 2
warning('不是所有的输出都分配了,有些输出不会被返回!')
end
%//调试
清除并返回N_ROWS NEWLINE VALS_PER_ROW文件路径
键盘; %//对于调试,完成后删除/注释。

结果输出是一个三维数组 uint32 (输出类可以通过调整输入到 textscan 来改变,如 formatSpec ):

  ans(:,:,1)= 

22 9 4 2
9 10 4 1
2 5 2 0
4 1 1 0


ans(:,:,2)=

14 7 0 4
8 4 0 2
1 2 0 0
1 1 1 1


I have input files containing data in the following format.

65910/A
22 9 4 2 
9 10 4 1 
2 5 2 0 
4 1 1 0 
65910/T
14 7 0 4 
8 4 0 2 
1 2 0 0 
1 1 1 1
.
.
. 

I need to take the input where the first line is a combination of %d and %c with a / in between and the next four line as a 4x4 integer matrix. I need to perform some work on the matrix and then identify them with the header information.

How can I take this input format in MATLAB?

解决方案

Since your file contains data that may be considered structured (or "formatted", if using MATLAB's terms), you can use the textscan function to read its contents. The main advantage of this function is that you don't need to specify how many times your "header+data" structure appears - the function just keeps going until it reaches the end of the file.

Given an input file with the following structure (let's call it q35853578.txt):

65910/A
22 9 4 2 
9 10 4 1 
2 5 2 0 
4 1 1 0 
65910/T
14 7 0 4 
8 4 0 2 
1 2 0 0 
1 1 1 1 

We can write something like this:

function [data,headers] = q35853578(filepath)
%// Default input
if nargin < 1
  filepath = 'q35853578.txt';
end
%// Define constants
N_ROWS = 4;
VALS_PER_ROW = 4;
NEWLINE = '\r\n';
%// Read structured file contents
fid = fopen(filepath);
headers = textscan(fid,['%u/%c' repmat([NEWLINE repmat('%u',1,VALS_PER_ROW)],1,N_ROWS)]);
fclose(fid);
%// Parse contents and prepare outputs
data = cell2mat(reshape(cellfun(@(x)reshape(x,1,1,[]),headers(3:end),...
  'UniformOutput',false),VALS_PER_ROW,N_ROWS).'); %'
headers = headers(1:2);
%// Output checking
if nargout < 2
  warning('Not all outputs assigned, some outputs will not be returned!')
end
%// Debug
clear ans fid N_ROWS NEWLINE VALS_PER_ROW filepath
keyboard; %// For debugging, delete/comment when done.

The resulting output is a 3d array of uint32 (the output class can be changed by adjusting the inputs to textscan, as permitted by formatSpec):

ans(:,:,1) =

          22           9           4           2
           9          10           4           1
           2           5           2           0
           4           1           1           0


ans(:,:,2) =

          14           7           0           4
           8           4           0           2
           1           2           0           0
           1           1           1           1

这篇关于Matlab输入格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆