我是作为一个没有Matlab经验的人来问这个问题的。在发表任何批评意见之前,请注意,我在这件事上绝对没有选择,我宁愿用我更熟悉的语言来做这个项目。
这个项目很简单;编写一个代码来分析一个简单的桁架。以下是一个团队成员一直在做的工作-我应该编写输入文件:

file = input('File Input): ', 's');
run(file)
%Cc corresponds to # of joints and Rc corresponds to # of members
[Rc Cc] = size(C);
%JM is a matrix that contains the joint numbers that each member is
%connected to. The row number is the member number
JM = zeros(Cc, 2);
%x components
Ax = zeros(Rc, Cc);
%y components
Ay = zeros(Rc, Cc);

My_total_length = 0;
Member_length = 0;

%finding JM matrix which stores the joints that each member is connected to
%loop through C matrix and store the joint number each member is connected
%to

for j = 1:Cc
    counter = 0;
    for i = 1:Rc
        if C(i,j) == 1
            counter = counter + 1;
            JM(j, counter) = i;
        end
    end
end

%using JM, loop through C again and create the equilibrium equations using
%the locations of each joint X and Y
for j = 1:Cc
    counter = 3;
    for i = 1:Rc
        if C(i,j) == 1
            %use counter to get x2 - x1 from JM vector
            counter = counter - 1;
            %find x and y distance of member and divide it by length of the member

            Member_length = sqrt(((X(JM(j,2))) - ...
            (X(JM(j,1))))^2 + ((Y(JM(j,2))) - (Y(JM(j,1))))^2);

            Ax(i,j) = (X(JM(j,counter)) - X(i)) / Member_length; ! 13!

            Ay(i,j)= (Y(JM(j, counter)) - Y(i)) / Member_length;
        end
    end
    My_total_length = My_total_length + Member_length;
end
%combine the 4 matrices to create the larger matrix A
A = [Ax Sx; Ay Sy];
%define the vector for the forces on each member
T = zeros(Cc +3, 1);
T = inv(A)*-L;

%OUTPUT

%Find what the load on the truss is by looping through the weight vector and finding the load
for i=1:length(L)
    if L(i) ~= 0
        Load = L(i);
    end
end

%Display load
fprintf('Load: %f N \n',Load)

%Display the forces on the members. Loop through Vector T up to the where reaction forces are
fprintf('Member forces in Newtons: \n')
for i = 1:length(T)-3
    if (T(i)<0)
        fprintf('m%d: %2.3f (C) \n', i, abs(T(i)))
    elseif (T(i)==0)
        fprintf('m%d: %d \n', i, abs(T(i)))
    else
         fprintf('m%d: %2.3f (T) \n', i, abs(T(i)))
    end
end

%Display the last three items of vector T, the reaction forces
fprintf('Reaction forces in Newtons: \nSx1: %2.3f \nSy1: %2.3f \nSy2: %2.3f \n'...
, T(length(T)-2), T(length(T)-1), T(end))

%Calculate cost of truss
Cost = Rc*(10) + My_total_length*(1);

%Display the cost of truss
fprintf('Cost of truss: $%3.2f \n', Cost)

%Display load/cost ratio
fprintf('Theoretical max load/cost ratio in N/$: %2.4f \n', (Load/Cost))

现在,我遇到的问题实际上是输入文件我应该将它保存为哪种类型的文件我怎么把它写进文件里当我试图运行代码并需要访问该文件时,如何编写文件名(后面带有.txt)文件中必须包含的一些信息如下:
Sx = [ 1 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0, 0 0 0; 0 0 0]
Sy = [ 0 1 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 1]
C = [ 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ; 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1]
L = [ 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 500; 0; 0; 0; 0; 0]
X = [ 0,6,12,18,24,30,36,42,48,54,60]
Y = [ 0,8,0,8,0,8,0,8,0,8,0]

非常感谢你的帮助。

最佳答案

您可以使用fwrite写入txt文件不过,这确实需要fread和代码来再次将其读入matlabxlswritexlsread对xls文件也一样。
但是,如果文件只与matlab一起使用,最简单的方法是使用函数loadsave您可以使用help savehelp load阅读帮助,但可以将一个或多个变量保存到.mat文件中。

a = 1;
b = 'e';
c = [1,2;3,4];
d = struct('hi',1,'you','e');
save('myFileName.mat','a','b','c','d');
clear;
load('myFileName.mat')

您还可以编写filepath/filename(或\for windows),其中filepath是完整路径。

关于algorithm - Matlab文件输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22834508/

10-12 21:17
查看更多