Sign up ×
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.


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

xmatrixx = Table[0, {100}, {100}];

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
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? – ciao May 22 '14 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. – Bstates May 22 '14 at 10:45

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: – Bstates May 22 '14 at 13:17

Your Answer


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.