Java Javascript reading files: how should I have asked?

AI Thread Summary
The discussion centers on the challenges of reading a data file into an array using JavaScript in a local web environment. The user initially struggled with this issue but found a workaround by having MATLAB generate a JavaScript file containing the necessary data. This approach allowed the user to call a function from the generated JavaScript file within their HTML page, successfully displaying the results in 3D.Key points include the realization that JavaScript typically operates in a browser sandbox, limiting its ability to read or write local files directly. Suggestions were made to reframe the original question to focus on loading data into an HTML page for use with a 3D renderer like Three.js, emphasizing the need for server-side processing. The discussion also highlighted the potential of using NodeJS to handle file operations, allowing for data computation and retrieval in a more effective manner. Overall, the conversation underscores the importance of understanding the environment in which JavaScript operates and how to effectively communicate technical needs.
Trying2Learn
Messages
375
Reaction score
57
TL;DR Summary
IO in javascript: what is the question
Hello

A few weeks ago (March 7) I asked how to have Javascript read a data file of numbers into an array.

I now understand why this is such a challenge when running a code on a web page, locally.

I have since found a sufficient workaround. I hope I can share it in the hope that someone can tell me how I SHOULD have asked the question. (Yes, this is an odd question: I am actually asking FOR a question.)

Here is what I did.

After Matlab processed all the numbers, I had Matlab open a file named "Rotation.js"
Matlab wrote the lines into Rotation.js to create an open of a function... then it deposited the data array.
Then it wrote the lines to "close the function."
Then it closed the file.

Then I ran the *.html file that ran the thereJS code. The main.js called the function that Matlab created, and displayed the
results in 3D.

So... I know that is a silly thing to have done (I KNOW I could have coded in OpenGL, or written the javascript code to process
the data. But considering the complexity of the Runge-Kutta method on six differential equations, this was a good workaround.

What SHOULD I have asked to have gotten this result? Again, i am not asking for an answer or even a better way. I just want to know how I could have asked the question.

Here is the section from Matlab to show you what I did.fileID = fopen('get_RotationMatrix.js','w');
fprintf(fileID,'function get_RotationMatrix(){\n');
fprintf(fileID,'var rotation = [\n');
for i = 1:timeIntegrationSteps
fprintf(fileID,'\"%12.8f\",\n',omega1(i));
end
fprintf(fileID,'];\n');
fprintf(fileID,'return rotation;\n}\n');
fclose(fileID);
 
Technology news on Phys.org
Nice workaround. In essence, you used a server-side program to create the necessary javascript containing your data that could then be retrieved by the browser and then processed on your web page.

In the context of javascript, your question is without meaning. I suppose if you had asked about processing data in Matlab and then how to display it on a web page you might gotten better responses but we'll never know as programming is such a diverse platform of recipes to do the same thing in different ways.

The problem with your question is that:
1) you are using javascript that traditionally works in a sandbox on a browser where local files simply can't be read or written.
2) you need to do things outside the browser to get it to work

NodeJS is a framework/server where you can use javascript on both the server-side and browser-side. In this case, javascript running on NodeJS can access files to read and to write.

You might read your binary data, compute stuff and then write out a JSON file with the data. There are methods that write data in JSON format for you.

The browser side of your application could request a computation and then retrieve the JSON file to process and display.
 
  • Like
Likes Trying2Learn and FactChecker
Trying2Learn said:
What SHOULD I have asked to have gotten this result?
How about "I want to load a data file into an HTML page (so I can use it as input to the 3D renderer Three.js), how can I do this? I want all this to be local, not through a web server or anything."
 
  • Like
Likes Trying2Learn
jedishrfu said:
The browser side of your application could request a computation and then retrieve the JSON file to process and display.
That won't work with a local file, the XMLHttpRequest api won't work with file:// and although a script tag will happily parse a JSON file there is no way to access the data in the user sandbox.
 
Last edited:
My apologies, that’s not what I meant the json data is sent back to the browser via the response to the request.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top