Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

There are $100$ .dat data files which contain numerical parameters.

m={1,2,.....,34};
n={8,10,12,.....19};
l={67,68,69,...96} 

and so on. I constructed a $100*100$ matrix with the code below:

xmatrixx = Table[0, {100}, {100}];
mflatten=Flatten[m];

Do[xmatrixx[[i, j]] = mflatten[[j]], {i, 1, 1}, {j, 1, 34}]];

But I want to put n and l and the other files in the other rows of the same matrix xmatrixx. How can I change the mflatten for obtaining n and l?

All the Best

share|improve this question
2  
Please, clarify what you want, with some more concrete examples and explanation. Then, read the documentation. Look at Import for how to get data into symbols, for example. No need to use a table to create empty matrix, use xmatrixx = ConstantArray[0, {100, 100}]; and the first replacement example is xmatrixx[[1, 1 ;; Length@m]] = m;, no need for a Do loop. What does the "String variable" in the title have to do with the question? Lastly, why Flatten something already flat? –  rasher May 22 at 10:07
    
Your comment was so useful. Ok I have gotten rid of a 'Do'. thank you so much for that. Following, I want to add some other files such as : 'n','l','o','p' each of which contains some other numerical parameters. For example (with your comment) I am able to replace zeros in the first row with 'm' contents but I want to add other files automatically to the matrix and in its rows. 'l'contents in second row, 'n' contents in the third row and so on. also I had used Flatten because my files are imported. –  mostafa May 22 at 10:45
add comment

1 Answer 1

up vote 2 down vote accepted

Suppose you have a list of filenames, perhaps by using the Filenames[] command.

Listofnames = {"file1.dat", "file2.dat"...}

You don't need to create an empty matrix and fill it in. You could try something like

mymatrix = With[{imported=Import[#]}, 
  Join[imported, ConstantArray[0, {100-Length[imported]}]]] & /@ Listofnames

Depending on the nature of your data file, you might need a Flatten around the Import command to ensure that you just have results (to be labeled as imported within the With statement) if the form {number, number...}.

The important point is that if you just want a matrix where each row comes from one of these files and the rest of the row up to 100 elements is filled in by zeros, this will work.

Bear in mind that this all assumes that your data files exist, contain data, and do not have lengths greater than or equal to 100. If you cannot guarantee that you will have to do more error-checking in your code.

share|improve this answer
    
Thank you so much for your guidance. Unfortunately I have a problem with Flattenning the imported files: –  mostafa May 22 at 13:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.