0

I want to create a text file like this with Matlab but i don't know how should I do that.

range(0,25e-9,0+600e-9),range(0+600e-9,1e-4,1.000000e-03),range(1.000000e-03,25e-9,1.000000e-03+600e-9),range(1.000000e-03+600e-9,1e-4,2.000000e-03),range(2.000000e-03,25e-9,2.000000e-03+600e-9),range(2.000000e-03+600e-9,1e-4,3.000000e-03)

for example here I want to create 6 point and I can do it by myself. But if I want create 100 point or 500 point I have to use Matlab. I wrote a code and create a matrix something like this but what I want is different. It's my code but I cant use it.....

clc
clear
close all

stp1=25e-9;
stp2=1e-4;
A=600e-9;
B=1e-3;

i=3;
F=zeros(i,3);

for i=1:i
    if i==1
        F(i,1)=0;
        F(i,2)=stp1;
        F(i,3)=A;
    else 
        if mod(i,2)==0
         F(i,1)=F(i-1,3);
         F(i,2)=stp2;
         F(i,3)=(i/2)*B;
        else
            F(i,1)=F(i-1,3);
            F(i,2)=stp1;
            F(i,3)=F(i,1)+A;
        end
    end
end

for example this is my matrix:

`     0.0000e+000    25.0000e-009   600.0000e-009
   600.0000e-009   100.0000e-006     1.0000e-003
     1.0000e-003    25.0000e-009     1.0006e-003`

I want to put them in one line and this like this:

`range(0.0000e+000,25.0000e-009,600.0000e-009),range(600.0000e-009,100.0000e-006,1.0000e-003),range(1.0000e-003,25.0000e-009,1.0006e-003)`

you know I want to add range(A(1,1),A(1,2),A(1,3)),range(A(2,1),A(2,2),A(2,3)) to my text file.... I hope I have explained well that I want.

10
  • Have you looked into fprintf and dlmwrite? What have you tried to write to a text file? Commented Nov 27, 2018 at 5:03
  • hmmm ... is your question about creating a text file ... or about creating the matrices with the desired numbers (and then put them in a text file) ?? Commented Nov 27, 2018 at 12:04
  • @SecretAgentMan @Hoki I want to create a text file like this: range(0,25e-9,0+600e-9),range(0+600e-9,1e-4,1.000000e-03), I can create a [i,3] matrix but i don't know how to put them in a text file like range(0,25e-9,0+600e-9),range(0+600e-9,1e-4,1.000000e-03),.... I don't know how to explain.... sorry :( Commented Nov 27, 2018 at 15:23
  • Suppose you have your m x n matrix A. (1) As @Hoki asked, do you know how to create your matrix? (2) You're wanting to write a text file from MATLAB that has matrix A, right? Is there a reason dlmwrite or fprintf aren't good approaches? (3) What have you tried so far (to write a text file)? Commented Nov 27, 2018 at 16:56
  • Also, this is tagged cell-array but I didn't see any cell arrays in your code. Can you explain why this tag applies? Commented Nov 27, 2018 at 16:57

1 Answer 1

1

I've put some code together below to help move this along. Please comment and I can adjust (or others can post answers based on updated information).

I'm still not sure exactly what outcome you're after.

For reference, you can see examples of file I/O documentation for dlmwrite here and for fprintf here. Notice you can specify the delimiter with dlmwrite and the exact format with fprintf.

A = [0.0000e+000    25.0000e-009   600.0000e-009;
 600.0000e-009   100.0000e-006     1.0000e-003;
 1.0000e-003    25.0000e-009     1.0006e-003];
dlmwrite('TestFile.txt',A)  % Example use of dlmwrite

B = range(A,2);             % Range of the rows of A
dlmwrite('TextFile2.txt',B)

C = cell(size(A,1),1);
fileID = fopen('TestFile3.txt','w+');
formatstr = '%12s\r\n';
for k = 1:size(A,1)
    C{k}=['range(A(' num2str(k) ',:)'];
    fprintf(fileID,formatstr,C{k});
end
fclose(fileID);

Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your help but the TextFile2.txt or TextFile1.txt did not include any range(.....) you know, for example this is our matrix link and I want to create a text file like this link. How can I add these strings to my text file??
@alieskandari, check the 3rd example. is this what you're after? I did it in column. Changing the formatstr can put it in rows.
imagine we have a matrix like this [1,2,3;4,5,6] and I want to create a text file like this: SecretAgentMan(1,2,3), SecretAgentMan(4,5,6) would you tell me how should I do that? @SecretAgentMan
@alieskandari, the command A(k,:) returns the k-th row of A. You can modify the code if you don't want to use MATLAB's "all-columns" shortcut ( the colon, : ). The code I posted seems to give you the answer you seek when I run it. Are you wanting it in row instead of column?
Thank you so much... I edited it and it works .... I really appreciate for your kindness. And I really sorry for bothered you and take your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.