0

So I have a text file, I read it into matlab using textscan now I have a big array that I need to divide into smaller arrays or matrices so I can use the numbers given to solve static equilibrium equation and find support reactions.

This is my code for scanning it in

function [C]= read_input(input)


   fid = fopen (input);
   C = textscan(fid,'%s', 'Delimiter', '\n', 'CommentStyle', '#');
   C = C{:};
   fclose(fid);

I get this as my C array

ans =

'2 1'
'0.0 1.0 1.0'
'5.0 3.0 0.0'
'11.0 3.0 2.0 -9.0'
'0.1 3.0 1.0 1.0'
'0.0 1.0 1.0'
'10.0 4.0 -2.0 9.0'
'1.0 1.0 1.0'
'1.0 1.0 1.0'
'1.0 1.0 1.0'
'0.0 1.0 0.0'
'0.0 1.0 1.0'
'1.0 1.0 0.0'
'F 1.0 6.0 -7.0'
'F 4.0 1.0 1.1'
'F 1.0 8.0 1.0'
'F 6.0 1.0 0.0'
'M 0.0 9.0 1.0'
'M -1.0 1.0 0.0'

and this was my original txt file

number of external forces and moments

2 1

coordinates of the points at which external forces are applied

x y z

0.0 1.0 1.0 5.0 3.0 0.0

magnitude and direction of external forces

F dx dy dz

11.0 3.0 2.0 -9.0 0.1 3.0 1.0 1.0

location at which external couple moments are applied

x y z

0.0 1.0 1.0

magnitude and direction of external couple moments

M ux uy uz

10.0 4.0 -2.0 9.0

location of supports

x y z

1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0

type (F/M) and direction of reaction

type dx dy uz

F 1.0 6.0 -7.0 F 4.0 1.0 1.1 F 1.0 8.0 1.0 F 6.0 1.0 0.0 M 0.0 9.0 1.0 M -1.0 1.0 0.0

4

1 回答 1

0

有两种方法可以解决这个问题。您可以输入为字符串元胞数组,然后转换为数字,也可以直接输入为数字。

1> 从单元格数组中获取数据必须是单独的,因为长度可变,并且不同的数量不会让您一次性输入。

temp = str2num(C{1,1}); 
num_forces= temp(1);
num_moments = temp(2);

2> 我更愿意通过输入本示例中给出的数字来解决它

input_text = textscan(fid,'%s', 1,'delimiter','\n'); % Read header line
HeaderLines{1}=input_text{1};
input_text = textscan(fid,'%f %f',1,'delimiter','\n');
[num_forces,num_moments] = input_text{1,:};

这将使您的代码更大但更易于管理。

于 2013-09-29T00:54:17.147 回答